blob: 43da911fc6e0d365c2d0359ba42e38bb814f4c33 [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 Jahanian1bf0afb2009-01-28 01:05:23 +0000636 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *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 Jahanian0e81f4b2009-02-05 20:41:40 +0000676
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000677public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000678 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000679 // FIXME. All stubs for now!
680 virtual llvm::Function *ModuleInitFunction();
681
682 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
683 QualType ResultType,
684 Selector Sel,
685 llvm::Value *Receiver,
686 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000687 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000688
689 virtual CodeGen::RValue
690 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
691 QualType ResultType,
692 Selector Sel,
693 const ObjCInterfaceDecl *Class,
694 llvm::Value *Receiver,
695 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000696 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000697
698 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000699 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000700
701 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
702 { return 0; }
703
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000704 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000705
706 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000707 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000708 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000709
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000710 virtual llvm::Function *GetPropertyGetFunction(){
711 return ObjCTypes.GetPropertyFn;
712 }
713 virtual llvm::Function *GetPropertySetFunction(){
714 return ObjCTypes.SetPropertyFn;
715 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000716 virtual llvm::Function *EnumerationMutationFunction()
717 { return 0; }
718
719 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
720 const Stmt &S)
721 { return; }
722 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
723 const ObjCAtThrowStmt &S)
724 { return; }
725 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
726 llvm::Value *AddrWeakObj)
727 { return 0; }
728 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
729 llvm::Value *src, llvm::Value *dst)
730 { return; }
731 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
732 llvm::Value *src, llvm::Value *dest)
733 { return; }
734 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
735 llvm::Value *src, llvm::Value *dest)
736 { return; }
737 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
738 llvm::Value *src, llvm::Value *dest)
739 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000740 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
741 QualType ObjectTy,
742 llvm::Value *BaseValue,
743 const ObjCIvarDecl *Ivar,
744 const FieldDecl *Field,
745 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000746 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
747 ObjCInterfaceDecl *Interface,
748 const ObjCIvarDecl *Ivar);
749 virtual bool LateBoundIVars() const { return true; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000750};
751
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000752} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000753
754/* *** Helper Functions *** */
755
756/// getConstantGEP() - Help routine to construct simple GEPs.
757static llvm::Constant *getConstantGEP(llvm::Constant *C,
758 unsigned idx0,
759 unsigned idx1) {
760 llvm::Value *Idxs[] = {
761 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
762 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
763 };
764 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
765}
766
767/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000768
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
770 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000771{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000772 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000773 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000774}
775
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000776/// GetClass - Return a reference to the class for the given interface
777/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000778llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000779 const ObjCInterfaceDecl *ID) {
780 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000781}
782
783/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000784llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000785 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000786}
787
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000788/// Generate a constant CFString object.
789/*
790 struct __builtin_CFString {
791 const int *isa; // point to __CFConstantStringClassReference
792 int flags;
793 const char *str;
794 long length;
795 };
796*/
797
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000798llvm::Constant *CGObjCCommonMac::GenerateConstantString(
799 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000800 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000801}
802
803/// Generates a message send where the super is the receiver. This is
804/// a message send to self with special delivery semantics indicating
805/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000806CodeGen::RValue
807CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000808 QualType ResultType,
809 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000810 const ObjCInterfaceDecl *Class,
811 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000812 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000813 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000814 // Create and init a super structure; this is a (receiver, class)
815 // pair we will pass to objc_msgSendSuper.
816 llvm::Value *ObjCSuper =
817 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
818 llvm::Value *ReceiverAsObject =
819 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
820 CGF.Builder.CreateStore(ReceiverAsObject,
821 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000822
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000823 // If this is a class message the metaclass is passed as the target.
824 llvm::Value *Target;
825 if (IsClassMessage) {
826 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
827 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
828 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
829 Target = Super;
830 } else {
831 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
832 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000833 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
834 // and ObjCTypes types.
835 const llvm::Type *ClassTy =
836 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000837 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000838 CGF.Builder.CreateStore(Target,
839 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
840
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000841 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000842 ObjCSuper, ObjCTypes.SuperPtrCTy,
843 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000844}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000845
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000846/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000847CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000848 QualType ResultType,
849 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000850 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000851 bool IsClassMessage,
852 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000853 llvm::Value *Arg0 =
854 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000855 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000856 Arg0, CGF.getContext().getObjCIdType(),
857 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000858}
859
860CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000861 QualType ResultType,
862 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000863 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000864 QualType Arg0Ty,
865 bool IsSuper,
866 const CallArgList &CallArgs) {
867 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000868 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
869 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
870 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000871 CGF.getContext().getObjCSelType()));
872 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000873
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000874 CodeGenTypes &Types = CGM.getTypes();
875 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
876 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000877
878 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000879 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000880 Fn = ObjCTypes.getSendStretFn(IsSuper);
881 } else if (ResultType->isFloatingType()) {
882 // FIXME: Sadly, this is wrong. This actually depends on the
883 // architecture. This happens to be right for x86-32 though.
884 Fn = ObjCTypes.getSendFpretFn(IsSuper);
885 } else {
886 Fn = ObjCTypes.getSendFn(IsSuper);
887 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000888 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000889 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000890}
891
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000892llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000893 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000894 // FIXME: I don't understand why gcc generates this, or where it is
895 // resolved. Investigate. Its also wasteful to look this up over and
896 // over.
897 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
898
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000899 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
900 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000901}
902
Fariborz Jahanianda320092009-01-29 19:24:30 +0000903void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000904 // FIXME: We shouldn't need this, the protocol decl should contain
905 // enough information to tell us whether this was a declaration or a
906 // definition.
907 DefinedProtocols.insert(PD->getIdentifier());
908
909 // If we have generated a forward reference to this protocol, emit
910 // it now. Otherwise do nothing, the protocol objects are lazily
911 // emitted.
912 if (Protocols.count(PD->getIdentifier()))
913 GetOrEmitProtocol(PD);
914}
915
Fariborz Jahanianda320092009-01-29 19:24:30 +0000916llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000917 if (DefinedProtocols.count(PD->getIdentifier()))
918 return GetOrEmitProtocol(PD);
919 return GetOrEmitProtocolRef(PD);
920}
921
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000922/*
923 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
924 struct _objc_protocol {
925 struct _objc_protocol_extension *isa;
926 char *protocol_name;
927 struct _objc_protocol_list *protocol_list;
928 struct _objc__method_prototype_list *instance_methods;
929 struct _objc__method_prototype_list *class_methods
930 };
931
932 See EmitProtocolExtension().
933*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000934llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
935 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
936
937 // Early exit if a defining object has already been generated.
938 if (Entry && Entry->hasInitializer())
939 return Entry;
940
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000941 // FIXME: I don't understand why gcc generates this, or where it is
942 // resolved. Investigate. Its also wasteful to look this up over and
943 // over.
944 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
945
Chris Lattner8ec03f52008-11-24 03:54:41 +0000946 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000947
948 // Construct method lists.
949 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
950 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
951 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
952 e = PD->instmeth_end(); i != e; ++i) {
953 ObjCMethodDecl *MD = *i;
954 llvm::Constant *C = GetMethodDescriptionConstant(MD);
955 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
956 OptInstanceMethods.push_back(C);
957 } else {
958 InstanceMethods.push_back(C);
959 }
960 }
961
962 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
963 e = PD->classmeth_end(); i != e; ++i) {
964 ObjCMethodDecl *MD = *i;
965 llvm::Constant *C = GetMethodDescriptionConstant(MD);
966 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
967 OptClassMethods.push_back(C);
968 } else {
969 ClassMethods.push_back(C);
970 }
971 }
972
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000973 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000975 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000976 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000977 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000978 PD->protocol_begin(),
979 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000980 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000981 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
982 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000983 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
984 InstanceMethods);
985 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000986 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
987 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000988 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
989 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
991 Values);
992
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000994 // Already created, fix the linkage and update the initializer.
995 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000996 Entry->setInitializer(Init);
997 } else {
998 Entry =
999 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1000 llvm::GlobalValue::InternalLinkage,
1001 Init,
1002 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1003 &CGM.getModule());
1004 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1005 UsedGlobals.push_back(Entry);
1006 // FIXME: Is this necessary? Why only for protocol?
1007 Entry->setAlignment(4);
1008 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001009
1010 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001011}
1012
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001013llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001014 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1015
1016 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001017 // We use the initializer as a marker of whether this is a forward
1018 // reference or not. At module finalization we add the empty
1019 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020 Entry =
1021 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001022 llvm::GlobalValue::ExternalLinkage,
1023 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001024 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001025 &CGM.getModule());
1026 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1027 UsedGlobals.push_back(Entry);
1028 // FIXME: Is this necessary? Why only for protocol?
1029 Entry->setAlignment(4);
1030 }
1031
1032 return Entry;
1033}
1034
1035/*
1036 struct _objc_protocol_extension {
1037 uint32_t size;
1038 struct objc_method_description_list *optional_instance_methods;
1039 struct objc_method_description_list *optional_class_methods;
1040 struct objc_property_list *instance_properties;
1041 };
1042*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001043llvm::Constant *
1044CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1045 const ConstantVector &OptInstanceMethods,
1046 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001047 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001048 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001049 std::vector<llvm::Constant*> Values(4);
1050 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001051 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001052 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1053 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001054 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1055 OptInstanceMethods);
1056 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001057 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1058 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001059 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1060 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001061 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1062 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001063 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001064
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001066 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1067 Values[3]->isNullValue())
1068 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1069
1070 llvm::Constant *Init =
1071 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1072 llvm::GlobalVariable *GV =
1073 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1074 llvm::GlobalValue::InternalLinkage,
1075 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001076 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001077 &CGM.getModule());
1078 // No special section, but goes in llvm.used
1079 UsedGlobals.push_back(GV);
1080
1081 return GV;
1082}
1083
1084/*
1085 struct objc_protocol_list {
1086 struct objc_protocol_list *next;
1087 long count;
1088 Protocol *list[];
1089 };
1090*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001091llvm::Constant *
1092CGObjCMac::EmitProtocolList(const std::string &Name,
1093 ObjCProtocolDecl::protocol_iterator begin,
1094 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001095 std::vector<llvm::Constant*> ProtocolRefs;
1096
Daniel Dunbardbc933702008-08-21 21:57:41 +00001097 for (; begin != end; ++begin)
1098 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001099
1100 // Just return null for empty protocol lists
1101 if (ProtocolRefs.empty())
1102 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1103
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001104 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001105 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1106
1107 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001108 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001109 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1110 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1111 Values[2] =
1112 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1113 ProtocolRefs.size()),
1114 ProtocolRefs);
1115
1116 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1117 llvm::GlobalVariable *GV =
1118 new llvm::GlobalVariable(Init->getType(), false,
1119 llvm::GlobalValue::InternalLinkage,
1120 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001121 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001122 &CGM.getModule());
1123 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1124 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1125}
1126
1127/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001128 struct _objc_property {
1129 const char * const name;
1130 const char * const attributes;
1131 };
1132
1133 struct _objc_property_list {
1134 uint32_t entsize; // sizeof (struct _objc_property)
1135 uint32_t prop_count;
1136 struct _objc_property[prop_count];
1137 };
1138*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001139llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1140 const Decl *Container,
1141 const ObjCContainerDecl *OCD,
1142 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001143 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001144 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1145 E = OCD->prop_end(); I != E; ++I) {
1146 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001147 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001148 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001149 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1150 Prop));
1151 }
1152
1153 // Return null for empty list.
1154 if (Properties.empty())
1155 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1156
1157 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001158 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001159 std::vector<llvm::Constant*> Values(3);
1160 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1161 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1162 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1163 Properties.size());
1164 Values[2] = llvm::ConstantArray::get(AT, Properties);
1165 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1166
1167 llvm::GlobalVariable *GV =
1168 new llvm::GlobalVariable(Init->getType(), false,
1169 llvm::GlobalValue::InternalLinkage,
1170 Init,
1171 Name,
1172 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001173 if (ObjCABI == 2)
1174 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001175 // No special section on property lists?
1176 UsedGlobals.push_back(GV);
1177 return llvm::ConstantExpr::getBitCast(GV,
1178 ObjCTypes.PropertyListPtrTy);
1179
1180}
1181
1182/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001183 struct objc_method_description_list {
1184 int count;
1185 struct objc_method_description list[];
1186 };
1187*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001188llvm::Constant *
1189CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1190 std::vector<llvm::Constant*> Desc(2);
1191 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1192 ObjCTypes.SelectorPtrTy);
1193 Desc[1] = GetMethodVarType(MD);
1194 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1195 Desc);
1196}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001197
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001198llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1199 const char *Section,
1200 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001201 // Return null for empty list.
1202 if (Methods.empty())
1203 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1204
1205 std::vector<llvm::Constant*> Values(2);
1206 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1207 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1208 Methods.size());
1209 Values[1] = llvm::ConstantArray::get(AT, Methods);
1210 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1211
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001212 llvm::GlobalVariable *GV =
1213 new llvm::GlobalVariable(Init->getType(), false,
1214 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001215 Init, Name, &CGM.getModule());
1216 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001217 UsedGlobals.push_back(GV);
1218 return llvm::ConstantExpr::getBitCast(GV,
1219 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001220}
1221
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001222/*
1223 struct _objc_category {
1224 char *category_name;
1225 char *class_name;
1226 struct _objc_method_list *instance_methods;
1227 struct _objc_method_list *class_methods;
1228 struct _objc_protocol_list *protocols;
1229 uint32_t size; // <rdar://4585769>
1230 struct _objc_property_list *instance_properties;
1231 };
1232 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001233void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001234 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001235
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001236 // FIXME: This is poor design, the OCD should have a pointer to the
1237 // category decl. Additionally, note that Category can be null for
1238 // the @implementation w/o an @interface case. Sema should just
1239 // create one for us as it does for @implementation so everyone else
1240 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001241 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001242 const ObjCCategoryDecl *Category =
1243 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001244 std::string ExtName(Interface->getNameAsString() + "_" +
1245 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001246
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001247 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1248 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1249 e = OCD->instmeth_end(); i != e; ++i) {
1250 // Instance methods should always be defined.
1251 InstanceMethods.push_back(GetMethodConstant(*i));
1252 }
1253 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1254 e = OCD->classmeth_end(); i != e; ++i) {
1255 // Class methods should always be defined.
1256 ClassMethods.push_back(GetMethodConstant(*i));
1257 }
1258
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001259 std::vector<llvm::Constant*> Values(7);
1260 Values[0] = GetClassName(OCD->getIdentifier());
1261 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001262 Values[2] =
1263 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1264 ExtName,
1265 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001266 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001267 Values[3] =
1268 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1269 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001270 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001271 if (Category) {
1272 Values[4] =
1273 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1274 Category->protocol_begin(),
1275 Category->protocol_end());
1276 } else {
1277 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1278 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001279 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001280
1281 // If there is no category @interface then there can be no properties.
1282 if (Category) {
1283 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001284 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001285 } else {
1286 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1287 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001288
1289 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1290 Values);
1291
1292 llvm::GlobalVariable *GV =
1293 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1294 llvm::GlobalValue::InternalLinkage,
1295 Init,
1296 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1297 &CGM.getModule());
1298 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1299 UsedGlobals.push_back(GV);
1300 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001301}
1302
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001303// FIXME: Get from somewhere?
1304enum ClassFlags {
1305 eClassFlags_Factory = 0x00001,
1306 eClassFlags_Meta = 0x00002,
1307 // <rdr://5142207>
1308 eClassFlags_HasCXXStructors = 0x02000,
1309 eClassFlags_Hidden = 0x20000,
1310 eClassFlags_ABI2_Hidden = 0x00010,
1311 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1312};
1313
1314// <rdr://5142207&4705298&4843145>
1315static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1316 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1317 // FIXME: Support -fvisibility
1318 switch (attr->getVisibility()) {
1319 default:
1320 assert(0 && "Unknown visibility");
1321 return false;
1322 case VisibilityAttr::DefaultVisibility:
1323 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1324 return false;
1325 case VisibilityAttr::HiddenVisibility:
1326 return true;
1327 }
1328 } else {
1329 return false; // FIXME: Support -fvisibility
1330 }
1331}
1332
1333/*
1334 struct _objc_class {
1335 Class isa;
1336 Class super_class;
1337 const char *name;
1338 long version;
1339 long info;
1340 long instance_size;
1341 struct _objc_ivar_list *ivars;
1342 struct _objc_method_list *methods;
1343 struct _objc_cache *cache;
1344 struct _objc_protocol_list *protocols;
1345 // Objective-C 1.0 extensions (<rdr://4585769>)
1346 const char *ivar_layout;
1347 struct _objc_class_ext *ext;
1348 };
1349
1350 See EmitClassExtension();
1351 */
1352void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001353 DefinedSymbols.insert(ID->getIdentifier());
1354
Chris Lattner8ec03f52008-11-24 03:54:41 +00001355 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001356 // FIXME: Gross
1357 ObjCInterfaceDecl *Interface =
1358 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001359 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001360 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001361 Interface->protocol_begin(),
1362 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001363 const llvm::Type *InterfaceTy =
1364 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1365 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001366 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001367
1368 // FIXME: Set CXX-structors flag.
1369 if (IsClassHidden(ID->getClassInterface()))
1370 Flags |= eClassFlags_Hidden;
1371
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001372 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1373 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1374 e = ID->instmeth_end(); i != e; ++i) {
1375 // Instance methods should always be defined.
1376 InstanceMethods.push_back(GetMethodConstant(*i));
1377 }
1378 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1379 e = ID->classmeth_end(); i != e; ++i) {
1380 // Class methods should always be defined.
1381 ClassMethods.push_back(GetMethodConstant(*i));
1382 }
1383
1384 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1385 e = ID->propimpl_end(); i != e; ++i) {
1386 ObjCPropertyImplDecl *PID = *i;
1387
1388 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1389 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1390
1391 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1392 if (llvm::Constant *C = GetMethodConstant(MD))
1393 InstanceMethods.push_back(C);
1394 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1395 if (llvm::Constant *C = GetMethodConstant(MD))
1396 InstanceMethods.push_back(C);
1397 }
1398 }
1399
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001400 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001401 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001402 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001403 // Record a reference to the super class.
1404 LazySymbols.insert(Super->getIdentifier());
1405
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001406 Values[ 1] =
1407 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1408 ObjCTypes.ClassPtrTy);
1409 } else {
1410 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1411 }
1412 Values[ 2] = GetClassName(ID->getIdentifier());
1413 // Version is always 0.
1414 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1415 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1416 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001417 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001418 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001419 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001420 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001421 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001422 // cache is always NULL.
1423 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1424 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001425 // FIXME: Set ivar_layout
1426 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001427 Values[11] = EmitClassExtension(ID);
1428 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1429 Values);
1430
1431 llvm::GlobalVariable *GV =
1432 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1433 llvm::GlobalValue::InternalLinkage,
1434 Init,
1435 std::string("\01L_OBJC_CLASS_")+ClassName,
1436 &CGM.getModule());
1437 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1438 UsedGlobals.push_back(GV);
1439 // FIXME: Why?
1440 GV->setAlignment(32);
1441 DefinedClasses.push_back(GV);
1442}
1443
1444llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1445 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001446 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001447 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001448 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001449 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001450
1451 if (IsClassHidden(ID->getClassInterface()))
1452 Flags |= eClassFlags_Hidden;
1453
1454 std::vector<llvm::Constant*> Values(12);
1455 // The isa for the metaclass is the root of the hierarchy.
1456 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1457 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1458 Root = Super;
1459 Values[ 0] =
1460 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1461 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001462 // The super class for the metaclass is emitted as the name of the
1463 // super class. The runtime fixes this up to point to the
1464 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001465 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1466 Values[ 1] =
1467 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1468 ObjCTypes.ClassPtrTy);
1469 } else {
1470 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1471 }
1472 Values[ 2] = GetClassName(ID->getIdentifier());
1473 // Version is always 0.
1474 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1475 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1476 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001477 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001478 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001479 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001480 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001481 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001482 // cache is always NULL.
1483 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1484 Values[ 9] = Protocols;
1485 // ivar_layout for metaclass is always NULL.
1486 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1487 // The class extension is always unused for metaclasses.
1488 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1489 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1490 Values);
1491
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001492 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001493 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001494
1495 // Check for a forward reference.
1496 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1497 if (GV) {
1498 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1499 "Forward metaclass reference has incorrect type.");
1500 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1501 GV->setInitializer(Init);
1502 } else {
1503 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1504 llvm::GlobalValue::InternalLinkage,
1505 Init, Name,
1506 &CGM.getModule());
1507 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001508 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1509 UsedGlobals.push_back(GV);
1510 // FIXME: Why?
1511 GV->setAlignment(32);
1512
1513 return GV;
1514}
1515
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001516llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001517 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001518
1519 // FIXME: Should we look these up somewhere other than the
1520 // module. Its a bit silly since we only generate these while
1521 // processing an implementation, so exactly one pointer would work
1522 // if know when we entered/exitted an implementation block.
1523
1524 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001525 // Previously, metaclass with internal linkage may have been defined.
1526 // pass 'true' as 2nd argument so it is returned.
1527 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001528 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1529 "Forward metaclass reference has incorrect type.");
1530 return GV;
1531 } else {
1532 // Generate as an external reference to keep a consistent
1533 // module. This will be patched up when we emit the metaclass.
1534 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1535 llvm::GlobalValue::ExternalLinkage,
1536 0,
1537 Name,
1538 &CGM.getModule());
1539 }
1540}
1541
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001542/*
1543 struct objc_class_ext {
1544 uint32_t size;
1545 const char *weak_ivar_layout;
1546 struct _objc_property_list *properties;
1547 };
1548*/
1549llvm::Constant *
1550CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1551 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001552 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001553
1554 std::vector<llvm::Constant*> Values(3);
1555 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001556 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001558 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001559 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001560
1561 // Return null if no extension bits are used.
1562 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1563 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1564
1565 llvm::Constant *Init =
1566 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1567 llvm::GlobalVariable *GV =
1568 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1569 llvm::GlobalValue::InternalLinkage,
1570 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001571 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001572 &CGM.getModule());
1573 // No special section, but goes in llvm.used
1574 UsedGlobals.push_back(GV);
1575
1576 return GV;
1577}
1578
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001579/// countInheritedIvars - count number of ivars in class and its super class(s)
1580///
1581static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1582 int count = 0;
1583 if (!OI)
1584 return 0;
1585 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1586 if (SuperClass)
1587 count += countInheritedIvars(SuperClass);
1588 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1589 E = OI->ivar_end(); I != E; ++I)
1590 ++count;
1591 return count;
1592}
1593
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001594/*
1595 struct objc_ivar {
1596 char *ivar_name;
1597 char *ivar_type;
1598 int ivar_offset;
1599 };
1600
1601 struct objc_ivar_list {
1602 int ivar_count;
1603 struct objc_ivar list[count];
1604 };
1605 */
1606llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001607 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001608 std::vector<llvm::Constant*> Ivars, Ivar(3);
1609
1610 // When emitting the root class GCC emits ivar entries for the
1611 // actual class structure. It is not clear if we need to follow this
1612 // behavior; for now lets try and get away with not doing it. If so,
1613 // the cleanest solution would be to make up an ObjCInterfaceDecl
1614 // for the class.
1615 if (ForClass)
1616 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001617
1618 ObjCInterfaceDecl *OID =
1619 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1620 const llvm::Type *InterfaceTy =
1621 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001622 const llvm::StructLayout *Layout =
1623 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001624
1625 RecordDecl::field_iterator ifield, pfield;
1626 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001627 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1628 FieldDecl *Field = *ifield;
1629 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1630 getLLVMFieldNo(Field));
1631 if (Field->getIdentifier())
1632 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1633 else
1634 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001635 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001636 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001637 Ivar[1] = GetMethodVarType(TypeStr);
1638 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001639 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001640 }
1641
1642 // Return null for empty list.
1643 if (Ivars.empty())
1644 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1645
1646 std::vector<llvm::Constant*> Values(2);
1647 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1648 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1649 Ivars.size());
1650 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1651 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1652
1653 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1654 "\01L_OBJC_INSTANCE_VARIABLES_");
1655 llvm::GlobalVariable *GV =
1656 new llvm::GlobalVariable(Init->getType(), false,
1657 llvm::GlobalValue::InternalLinkage,
1658 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001659 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001660 &CGM.getModule());
1661 if (ForClass) {
1662 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1663 // FIXME: Why is this only here?
1664 GV->setAlignment(32);
1665 } else {
1666 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1667 }
1668 UsedGlobals.push_back(GV);
1669 return llvm::ConstantExpr::getBitCast(GV,
1670 ObjCTypes.IvarListPtrTy);
1671}
1672
1673/*
1674 struct objc_method {
1675 SEL method_name;
1676 char *method_types;
1677 void *method;
1678 };
1679
1680 struct objc_method_list {
1681 struct objc_method_list *obsolete;
1682 int count;
1683 struct objc_method methods_list[count];
1684 };
1685*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001686
1687/// GetMethodConstant - Return a struct objc_method constant for the
1688/// given method if it has been defined. The result is null if the
1689/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001690llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001691 // FIXME: Use DenseMap::lookup
1692 llvm::Function *Fn = MethodDefinitions[MD];
1693 if (!Fn)
1694 return 0;
1695
1696 std::vector<llvm::Constant*> Method(3);
1697 Method[0] =
1698 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1699 ObjCTypes.SelectorPtrTy);
1700 Method[1] = GetMethodVarType(MD);
1701 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1702 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1703}
1704
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001705llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1706 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001707 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001708 // Return null for empty list.
1709 if (Methods.empty())
1710 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1711
1712 std::vector<llvm::Constant*> Values(3);
1713 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1714 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1715 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1716 Methods.size());
1717 Values[2] = llvm::ConstantArray::get(AT, Methods);
1718 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1719
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001720 llvm::GlobalVariable *GV =
1721 new llvm::GlobalVariable(Init->getType(), false,
1722 llvm::GlobalValue::InternalLinkage,
1723 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001724 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001725 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001726 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001727 UsedGlobals.push_back(GV);
1728 return llvm::ConstantExpr::getBitCast(GV,
1729 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001730}
1731
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001732llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001733 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001734 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001735 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001736
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001737 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001738 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001739 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001740 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001741 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001742 llvm::GlobalValue::InternalLinkage,
1743 Name,
1744 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001745 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001746
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001747 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001748}
1749
1750llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001751 // Abuse this interface function as a place to finalize.
1752 FinishModule();
1753
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001754 return NULL;
1755}
1756
Daniel Dunbar49f66022008-09-24 03:38:44 +00001757llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1758 return ObjCTypes.GetPropertyFn;
1759}
1760
1761llvm::Function *CGObjCMac::GetPropertySetFunction() {
1762 return ObjCTypes.SetPropertyFn;
1763}
1764
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001765llvm::Function *CGObjCMac::EnumerationMutationFunction()
1766{
1767 return ObjCTypes.EnumerationMutationFn;
1768}
1769
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001770/*
1771
1772Objective-C setjmp-longjmp (sjlj) Exception Handling
1773--
1774
1775The basic framework for a @try-catch-finally is as follows:
1776{
1777 objc_exception_data d;
1778 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001779 bool _call_try_exit = true;
1780
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001781 objc_exception_try_enter(&d);
1782 if (!setjmp(d.jmp_buf)) {
1783 ... try body ...
1784 } else {
1785 // exception path
1786 id _caught = objc_exception_extract(&d);
1787
1788 // enter new try scope for handlers
1789 if (!setjmp(d.jmp_buf)) {
1790 ... match exception and execute catch blocks ...
1791
1792 // fell off end, rethrow.
1793 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001794 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001795 } else {
1796 // exception in catch block
1797 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001798 _call_try_exit = false;
1799 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001800 }
1801 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001802 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001803
1804finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001805 if (_call_try_exit)
1806 objc_exception_try_exit(&d);
1807
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001808 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001809 ... dispatch to finally destination ...
1810
1811finally_rethrow:
1812 objc_exception_throw(_rethrow);
1813
1814finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001815}
1816
1817This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001818uses _rethrow to determine if objc_exception_try_exit should be called
1819and if the object should be rethrown. This breaks in the face of
1820throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001821
1822We specialize this framework for a few particular circumstances:
1823
1824 - If there are no catch blocks, then we avoid emitting the second
1825 exception handling context.
1826
1827 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1828 e)) we avoid emitting the code to rethrow an uncaught exception.
1829
1830 - FIXME: If there is no @finally block we can do a few more
1831 simplifications.
1832
1833Rethrows and Jumps-Through-Finally
1834--
1835
1836Support for implicit rethrows and jumping through the finally block is
1837handled by storing the current exception-handling context in
1838ObjCEHStack.
1839
Daniel Dunbar898d5082008-09-30 01:06:03 +00001840In order to implement proper @finally semantics, we support one basic
1841mechanism for jumping through the finally block to an arbitrary
1842destination. Constructs which generate exits from a @try or @catch
1843block use this mechanism to implement the proper semantics by chaining
1844jumps, as necessary.
1845
1846This mechanism works like the one used for indirect goto: we
1847arbitrarily assign an ID to each destination and store the ID for the
1848destination in a variable prior to entering the finally block. At the
1849end of the finally block we simply create a switch to the proper
1850destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001851
1852Code gen for @synchronized(expr) stmt;
1853Effectively generating code for:
1854objc_sync_enter(expr);
1855@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001856*/
1857
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001858void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1859 const Stmt &S) {
1860 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001861 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001862 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001863 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001864 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1865 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1866 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001867
1868 // Push an EH context entry, used for handling rethrows and jumps
1869 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001870 CGF.PushCleanupBlock(FinallyBlock);
1871
Anders Carlsson273558f2009-02-07 21:37:21 +00001872 CGF.ObjCEHValueStack.push_back(0);
1873
Daniel Dunbar898d5082008-09-30 01:06:03 +00001874 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001875 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1876 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001877 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1878 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001879 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1880 "_call_try_exit");
1881 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1882
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001883 if (!isTry) {
1884 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001885 llvm::Value *Arg = CGF.EmitScalarExpr(
1886 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1887 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1888 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001889 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001890
1891 // Enter a new try block and call setjmp.
1892 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1893 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1894 "jmpbufarray");
1895 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1896 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1897 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001898
Daniel Dunbar55e87422008-11-11 02:29:29 +00001899 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1900 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001901 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001902 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001903
1904 // Emit the @try block.
1905 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001906 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1907 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001908 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001909
1910 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001911 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001912
1913 // Retrieve the exception object. We may emit multiple blocks but
1914 // nothing can cross this so the value is already in SSA form.
1915 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1916 ExceptionData,
1917 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001918 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001919 if (!isTry)
1920 {
1921 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001922 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001923 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001924 }
1925 else if (const ObjCAtCatchStmt* CatchStmt =
1926 cast<ObjCAtTryStmt>(S).getCatchStmts())
1927 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001928 // Enter a new exception try block (in case a @catch block throws
1929 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001930 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001931
Anders Carlsson80f25672008-09-09 17:59:25 +00001932 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1933 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001934 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001935
Daniel Dunbar55e87422008-11-11 02:29:29 +00001936 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1937 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001938 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001939
1940 CGF.EmitBlock(CatchBlock);
1941
Daniel Dunbar55e40722008-09-27 07:03:52 +00001942 // Handle catch list. As a special case we check if everything is
1943 // matched and avoid generating code for falling off the end if
1944 // so.
1945 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001946 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001947 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001948
Anders Carlssondde0a942008-09-11 09:15:33 +00001949 const DeclStmt *CatchParam =
1950 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001951 const VarDecl *VD = 0;
1952 const PointerType *PT = 0;
1953
Anders Carlsson80f25672008-09-09 17:59:25 +00001954 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001955 if (!CatchParam) {
1956 AllMatched = true;
1957 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001958 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001959 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001960
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001961 // catch(id e) always matches.
1962 // FIXME: For the time being we also match id<X>; this should
1963 // be rejected by Sema instead.
1964 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1965 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001966 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001967 }
1968
Daniel Dunbar55e40722008-09-27 07:03:52 +00001969 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001970 if (CatchParam) {
1971 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001972 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001973 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001974 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001975
Anders Carlssondde0a942008-09-11 09:15:33 +00001976 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001977 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001978 break;
1979 }
1980
Daniel Dunbar129271a2008-09-27 07:36:24 +00001981 assert(PT && "Unexpected non-pointer type in @catch");
1982 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001983 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001984 assert(ObjCType && "Catch parameter must have Objective-C type!");
1985
1986 // Check if the @catch block matches the exception object.
1987 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1988
Anders Carlsson80f25672008-09-09 17:59:25 +00001989 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1990 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001991
Daniel Dunbar55e87422008-11-11 02:29:29 +00001992 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001993
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001994 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001995 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001996
1997 // Emit the @catch block.
1998 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001999 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002000 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002001
2002 llvm::Value *Tmp =
2003 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2004 "tmp");
2005 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002006
2007 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002008 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002009
2010 CGF.EmitBlock(NextCatchBlock);
2011 }
2012
Daniel Dunbar55e40722008-09-27 07:03:52 +00002013 if (!AllMatched) {
2014 // None of the handlers caught the exception, so store it to be
2015 // rethrown at the end of the @finally block.
2016 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002017 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002018 }
2019
2020 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002021 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002022 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2023 ExceptionData),
2024 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002025 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002026 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002027 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002028 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002029 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002030 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002031 }
2032
Daniel Dunbar898d5082008-09-30 01:06:03 +00002033 // Pop the exception-handling stack entry. It is important to do
2034 // this now, because the code in the @finally block is not in this
2035 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002036 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2037
Anders Carlsson273558f2009-02-07 21:37:21 +00002038 CGF.ObjCEHValueStack.pop_back();
2039
Anders Carlsson80f25672008-09-09 17:59:25 +00002040 // Emit the @finally block.
2041 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002042 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2043
2044 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2045
2046 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002047 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002048
2049 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002050 if (isTry) {
2051 if (const ObjCAtFinallyStmt* FinallyStmt =
2052 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2053 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2054 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002055 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002056 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002057 // For @synchronized, call objc_sync_enter(sync.expr)
2058 llvm::Value *Arg = CGF.EmitScalarExpr(
2059 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2060 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2061 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2062 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002063
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002064 // Emit the switch block
2065 if (Info.SwitchBlock)
2066 CGF.EmitBlock(Info.SwitchBlock);
2067 if (Info.EndBlock)
2068 CGF.EmitBlock(Info.EndBlock);
2069
Daniel Dunbar898d5082008-09-30 01:06:03 +00002070 CGF.EmitBlock(FinallyRethrow);
2071 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2072 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002073 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002074
2075 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002076}
2077
2078void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002079 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002080 llvm::Value *ExceptionAsObject;
2081
2082 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2083 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2084 ExceptionAsObject =
2085 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2086 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002087 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002088 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002089 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002090 }
2091
2092 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002093 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002094
2095 // Clear the insertion point to indicate we are in unreachable code.
2096 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002097}
2098
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002099/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002100/// object: objc_read_weak (id *src)
2101///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002102llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002103 llvm::Value *AddrWeakObj)
2104{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002105 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002106 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002107 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002108 return read_weak;
2109}
2110
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002111/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2112/// objc_assign_weak (id src, id *dst)
2113///
2114void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2115 llvm::Value *src, llvm::Value *dst)
2116{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002117 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2118 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002119 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2120 src, dst, "weakassign");
2121 return;
2122}
2123
Fariborz Jahanian58626502008-11-19 00:59:10 +00002124/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2125/// objc_assign_global (id src, id *dst)
2126///
2127void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2128 llvm::Value *src, llvm::Value *dst)
2129{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002130 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2131 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002132 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2133 src, dst, "globalassign");
2134 return;
2135}
2136
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002137/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2138/// objc_assign_ivar (id src, id *dst)
2139///
2140void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2141 llvm::Value *src, llvm::Value *dst)
2142{
2143 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2144 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2145 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2146 src, dst, "assignivar");
2147 return;
2148}
2149
Fariborz Jahanian58626502008-11-19 00:59:10 +00002150/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2151/// objc_assign_strongCast (id src, id *dst)
2152///
2153void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2154 llvm::Value *src, llvm::Value *dst)
2155{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002156 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2157 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002158 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2159 src, dst, "weakassign");
2160 return;
2161}
2162
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002163/// EmitObjCValueForIvar - Code Gen for ivar reference.
2164///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002165LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2166 QualType ObjectTy,
2167 llvm::Value *BaseValue,
2168 const ObjCIvarDecl *Ivar,
2169 const FieldDecl *Field,
2170 unsigned CVRQualifiers) {
2171 if (Ivar->isBitField())
2172 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2173 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002174 // TODO: Add a special case for isa (index 0)
2175 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2176 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002177 LValue LV = LValue::MakeAddr(V,
2178 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2179 LValue::SetObjCIvar(LV, true);
2180 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002181}
2182
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002183llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2184 ObjCInterfaceDecl *Interface,
2185 const ObjCIvarDecl *Ivar) {
2186 const llvm::Type *InterfaceLTy =
2187 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2188 const llvm::StructLayout *Layout =
2189 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2190 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2191 uint64_t Offset =
2192 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2193
2194 return llvm::ConstantInt::get(
2195 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2196 Offset);
2197}
2198
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002199/* *** Private Interface *** */
2200
2201/// EmitImageInfo - Emit the image info marker used to encode some module
2202/// level information.
2203///
2204/// See: <rdr://4810609&4810587&4810587>
2205/// struct IMAGE_INFO {
2206/// unsigned version;
2207/// unsigned flags;
2208/// };
2209enum ImageInfoFlags {
2210 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2211 eImageInfo_GarbageCollected = (1 << 1),
2212 eImageInfo_GCOnly = (1 << 2)
2213};
2214
2215void CGObjCMac::EmitImageInfo() {
2216 unsigned version = 0; // Version is unused?
2217 unsigned flags = 0;
2218
2219 // FIXME: Fix and continue?
2220 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2221 flags |= eImageInfo_GarbageCollected;
2222 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2223 flags |= eImageInfo_GCOnly;
2224
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002225 // Emitted as int[2];
2226 llvm::Constant *values[2] = {
2227 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2228 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2229 };
2230 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002231 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002232 new llvm::GlobalVariable(AT, true,
2233 llvm::GlobalValue::InternalLinkage,
2234 llvm::ConstantArray::get(AT, values, 2),
2235 "\01L_OBJC_IMAGE_INFO",
2236 &CGM.getModule());
2237
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002238 if (ObjCABI == 1) {
2239 GV->setSection("__OBJC, __image_info,regular");
2240 } else {
2241 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2242 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002243
2244 UsedGlobals.push_back(GV);
2245}
2246
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002247
2248// struct objc_module {
2249// unsigned long version;
2250// unsigned long size;
2251// const char *name;
2252// Symtab symtab;
2253// };
2254
2255// FIXME: Get from somewhere
2256static const int ModuleVersion = 7;
2257
2258void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002259 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002260
2261 std::vector<llvm::Constant*> Values(4);
2262 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2263 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002264 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002265 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002266 Values[3] = EmitModuleSymbols();
2267
2268 llvm::GlobalVariable *GV =
2269 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2270 llvm::GlobalValue::InternalLinkage,
2271 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2272 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002273 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002274 &CGM.getModule());
2275 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2276 UsedGlobals.push_back(GV);
2277}
2278
2279llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 unsigned NumClasses = DefinedClasses.size();
2281 unsigned NumCategories = DefinedCategories.size();
2282
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002283 // Return null if no symbols were defined.
2284 if (!NumClasses && !NumCategories)
2285 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2286
2287 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002288 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2289 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2290 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2291 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2292
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002293 // The runtime expects exactly the list of defined classes followed
2294 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002295 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002296 for (unsigned i=0; i<NumClasses; i++)
2297 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2298 ObjCTypes.Int8PtrTy);
2299 for (unsigned i=0; i<NumCategories; i++)
2300 Symbols[NumClasses + i] =
2301 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2302 ObjCTypes.Int8PtrTy);
2303
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002304 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002305 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 NumClasses + NumCategories),
2307 Symbols);
2308
2309 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2310
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002311 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002313 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002314 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002315 "\01L_OBJC_SYMBOLS",
2316 &CGM.getModule());
2317 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2318 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002319 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2320}
2321
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002322llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002324 LazySymbols.insert(ID->getIdentifier());
2325
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2327
2328 if (!Entry) {
2329 llvm::Constant *Casted =
2330 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2331 ObjCTypes.ClassPtrTy);
2332 Entry =
2333 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2334 llvm::GlobalValue::InternalLinkage,
2335 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2336 &CGM.getModule());
2337 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2338 UsedGlobals.push_back(Entry);
2339 }
2340
2341 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002342}
2343
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002344llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002345 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2346
2347 if (!Entry) {
2348 llvm::Constant *Casted =
2349 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2350 ObjCTypes.SelectorPtrTy);
2351 Entry =
2352 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2353 llvm::GlobalValue::InternalLinkage,
2354 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2355 &CGM.getModule());
2356 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2357 UsedGlobals.push_back(Entry);
2358 }
2359
2360 return Builder.CreateLoad(Entry, false, "tmp");
2361}
2362
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002363llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002364 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002365
2366 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002367 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002368 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002369 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002370 llvm::GlobalValue::InternalLinkage,
2371 C, "\01L_OBJC_CLASS_NAME_",
2372 &CGM.getModule());
2373 Entry->setSection("__TEXT,__cstring,cstring_literals");
2374 UsedGlobals.push_back(Entry);
2375 }
2376
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002377 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002378}
2379
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002380llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002381 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2382
2383 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002384 // FIXME: Avoid std::string copying.
2385 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002386 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002387 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002388 llvm::GlobalValue::InternalLinkage,
2389 C, "\01L_OBJC_METH_VAR_NAME_",
2390 &CGM.getModule());
2391 Entry->setSection("__TEXT,__cstring,cstring_literals");
2392 UsedGlobals.push_back(Entry);
2393 }
2394
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002395 return getConstantGEP(Entry, 0, 0);
2396}
2397
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002398// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002399llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2401}
2402
2403// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002404llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002405 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2406}
2407
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002408llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002409 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002410
2411 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002412 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002413 Entry =
2414 new llvm::GlobalVariable(C->getType(), false,
2415 llvm::GlobalValue::InternalLinkage,
2416 C, "\01L_OBJC_METH_VAR_TYPE_",
2417 &CGM.getModule());
2418 Entry->setSection("__TEXT,__cstring,cstring_literals");
2419 UsedGlobals.push_back(Entry);
2420 }
2421
2422 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002423}
2424
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002425// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002426llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002428 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2429 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 return GetMethodVarType(TypeStr);
2431}
2432
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002433// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002434llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002435 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2436
2437 if (!Entry) {
2438 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2439 Entry =
2440 new llvm::GlobalVariable(C->getType(), false,
2441 llvm::GlobalValue::InternalLinkage,
2442 C, "\01L_OBJC_PROP_NAME_ATTR_",
2443 &CGM.getModule());
2444 Entry->setSection("__TEXT,__cstring,cstring_literals");
2445 UsedGlobals.push_back(Entry);
2446 }
2447
2448 return getConstantGEP(Entry, 0, 0);
2449}
2450
2451// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002452// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002453llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002454 const Decl *Container) {
2455 std::string TypeStr;
2456 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002457 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2458}
2459
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002460void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2461 const ObjCContainerDecl *CD,
2462 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002463 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002464 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002465 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002466 assert (CD && "Missing container decl in GetNameForMethod");
2467 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002468 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2469 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002470 NameOut += ' ';
2471 NameOut += D->getSelector().getAsString();
2472 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002473}
2474
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002475/// GetFirstIvarInRecord - This routine returns the record for the
2476/// implementation of the fiven class OID. It also returns field
2477/// corresponding to the first ivar in the class in FIV. It also
2478/// returns the one before the first ivar.
2479///
2480const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2481 const ObjCInterfaceDecl *OID,
2482 RecordDecl::field_iterator &FIV,
2483 RecordDecl::field_iterator &PIV) {
2484 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2485 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2486 RecordDecl::field_iterator ifield = RD->field_begin();
2487 RecordDecl::field_iterator pfield = RD->field_end();
2488 while (countSuperClassIvars-- > 0) {
2489 pfield = ifield;
2490 ++ifield;
2491 }
2492 FIV = ifield;
2493 PIV = pfield;
2494 return RD;
2495}
2496
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002497void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002498 EmitModuleInfo();
2499
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002500 // Emit the dummy bodies for any protocols which were referenced but
2501 // never defined.
2502 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2503 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2504 if (i->second->hasInitializer())
2505 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002506
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002507 std::vector<llvm::Constant*> Values(5);
2508 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2509 Values[1] = GetClassName(i->first);
2510 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2511 Values[3] = Values[4] =
2512 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2513 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2514 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2515 Values));
2516 }
2517
2518 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002519 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002520 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002521 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002522 }
2523
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002524 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002525 llvm::GlobalValue *GV =
2526 new llvm::GlobalVariable(AT, false,
2527 llvm::GlobalValue::AppendingLinkage,
2528 llvm::ConstantArray::get(AT, Used),
2529 "llvm.used",
2530 &CGM.getModule());
2531
2532 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002533
2534 // Add assembler directives to add lazy undefined symbol references
2535 // for classes which are referenced but not defined. This is
2536 // important for correct linker interaction.
2537
2538 // FIXME: Uh, this isn't particularly portable.
2539 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002540
2541 if (!CGM.getModule().getModuleInlineAsm().empty())
2542 s << "\n";
2543
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002544 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2545 e = LazySymbols.end(); i != e; ++i) {
2546 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2547 }
2548 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2549 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002550 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002551 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2552 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002553
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002554 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002555}
2556
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002557CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002558 : CGObjCCommonMac(cgm),
2559 ObjCTypes(cgm)
2560{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002561 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002562 ObjCABI = 2;
2563}
2564
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002565/* *** */
2566
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002567ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2568: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002569{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002570 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2571 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002572
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002573 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002574 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002575 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002576 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2577
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002578 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002579 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002580 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002581
2582 // FIXME: It would be nice to unify this with the opaque type, so
2583 // that the IR comes out a bit cleaner.
2584 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2585 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002586
2587 // I'm not sure I like this. The implicit coordination is a bit
2588 // gross. We should solve this in a reasonable fashion because this
2589 // is a pretty common task (match some runtime data structure with
2590 // an LLVM data structure).
2591
2592 // FIXME: This is leaked.
2593 // FIXME: Merge with rewriter code?
2594
2595 // struct _objc_super {
2596 // id self;
2597 // Class cls;
2598 // }
2599 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2600 SourceLocation(),
2601 &Ctx.Idents.get("_objc_super"));
2602 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2603 Ctx.getObjCIdType(), 0, false));
2604 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2605 Ctx.getObjCClassType(), 0, false));
2606 RD->completeDefinition(Ctx);
2607
2608 SuperCTy = Ctx.getTagDeclType(RD);
2609 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2610
2611 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002612 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2613
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002614 // struct _prop_t {
2615 // char *name;
2616 // char *attributes;
2617 // }
2618 PropertyTy = llvm::StructType::get(Int8PtrTy,
2619 Int8PtrTy,
2620 NULL);
2621 CGM.getModule().addTypeName("struct._prop_t",
2622 PropertyTy);
2623
2624 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002625 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002626 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002627 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002628 // }
2629 PropertyListTy = llvm::StructType::get(IntTy,
2630 IntTy,
2631 llvm::ArrayType::get(PropertyTy, 0),
2632 NULL);
2633 CGM.getModule().addTypeName("struct._prop_list_t",
2634 PropertyListTy);
2635 // struct _prop_list_t *
2636 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2637
2638 // struct _objc_method {
2639 // SEL _cmd;
2640 // char *method_type;
2641 // char *_imp;
2642 // }
2643 MethodTy = llvm::StructType::get(SelectorPtrTy,
2644 Int8PtrTy,
2645 Int8PtrTy,
2646 NULL);
2647 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002648
2649 // struct _objc_cache *
2650 CacheTy = llvm::OpaqueType::get();
2651 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2652 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002653
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002654 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002655
2656 QualType IdType = Ctx.getObjCIdType();
2657 QualType SelType = Ctx.getObjCSelType();
2658 llvm::SmallVector<QualType,16> Params;
2659 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002660
2661 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002662 Params.push_back(IdType);
2663 Params.push_back(SelType);
2664 Params.push_back(Ctx.LongTy);
2665 Params.push_back(Ctx.BoolTy);
2666 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2667 false);
2668 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002669
2670 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2671 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002672 Params.push_back(IdType);
2673 Params.push_back(SelType);
2674 Params.push_back(Ctx.LongTy);
2675 Params.push_back(IdType);
2676 Params.push_back(Ctx.BoolTy);
2677 Params.push_back(Ctx.BoolTy);
2678 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2679 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2680
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002681 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002682
2683 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002684 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002685 Params.push_back(IdType);
2686 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2687 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2688 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002689
2690 // gc's API
2691 // id objc_read_weak (id *)
2692 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002693 Params.push_back(Ctx.getPointerType(IdType));
2694 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2695 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2696
2697 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002698 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002699 Params.push_back(IdType);
2700 Params.push_back(Ctx.getPointerType(IdType));
2701
2702 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2703 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2704 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2705 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2706 GcAssignStrongCastFn =
2707 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002708}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002709
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002710ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2711 : ObjCCommonTypesHelper(cgm)
2712{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002713 // struct _objc_method_description {
2714 // SEL name;
2715 // char *types;
2716 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002717 MethodDescriptionTy =
2718 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002719 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002720 NULL);
2721 CGM.getModule().addTypeName("struct._objc_method_description",
2722 MethodDescriptionTy);
2723
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002724 // struct _objc_method_description_list {
2725 // int count;
2726 // struct _objc_method_description[1];
2727 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002728 MethodDescriptionListTy =
2729 llvm::StructType::get(IntTy,
2730 llvm::ArrayType::get(MethodDescriptionTy, 0),
2731 NULL);
2732 CGM.getModule().addTypeName("struct._objc_method_description_list",
2733 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002734
2735 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002736 MethodDescriptionListPtrTy =
2737 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2738
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002739 // Protocol description structures
2740
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002741 // struct _objc_protocol_extension {
2742 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2743 // struct _objc_method_description_list *optional_instance_methods;
2744 // struct _objc_method_description_list *optional_class_methods;
2745 // struct _objc_property_list *instance_properties;
2746 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002747 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002748 llvm::StructType::get(IntTy,
2749 MethodDescriptionListPtrTy,
2750 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002751 PropertyListPtrTy,
2752 NULL);
2753 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2754 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002755
2756 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002757 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2758
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002759 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002760
2761 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2762 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2763
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002764 const llvm::Type *T =
2765 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2766 LongTy,
2767 llvm::ArrayType::get(ProtocolTyHolder, 0),
2768 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002769 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2770
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002771 // struct _objc_protocol {
2772 // struct _objc_protocol_extension *isa;
2773 // char *protocol_name;
2774 // struct _objc_protocol **_objc_protocol_list;
2775 // struct _objc_method_description_list *instance_methods;
2776 // struct _objc_method_description_list *class_methods;
2777 // }
2778 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002779 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002780 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2781 MethodDescriptionListPtrTy,
2782 MethodDescriptionListPtrTy,
2783 NULL);
2784 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2785
2786 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2787 CGM.getModule().addTypeName("struct._objc_protocol_list",
2788 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002789 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002790 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2791
2792 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002793 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002794 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002795
2796 // Class description structures
2797
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002798 // struct _objc_ivar {
2799 // char *ivar_name;
2800 // char *ivar_type;
2801 // int ivar_offset;
2802 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002803 IvarTy = llvm::StructType::get(Int8PtrTy,
2804 Int8PtrTy,
2805 IntTy,
2806 NULL);
2807 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2808
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002809 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002810 IvarListTy = llvm::OpaqueType::get();
2811 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2812 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2813
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002814 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002815 MethodListTy = llvm::OpaqueType::get();
2816 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2817 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2818
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002819 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002820 ClassExtensionTy =
2821 llvm::StructType::get(IntTy,
2822 Int8PtrTy,
2823 PropertyListPtrTy,
2824 NULL);
2825 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2826 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2827
2828 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2829
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002830 // struct _objc_class {
2831 // Class isa;
2832 // Class super_class;
2833 // char *name;
2834 // long version;
2835 // long info;
2836 // long instance_size;
2837 // struct _objc_ivar_list *ivars;
2838 // struct _objc_method_list *methods;
2839 // struct _objc_cache *cache;
2840 // struct _objc_protocol_list *protocols;
2841 // char *ivar_layout;
2842 // struct _objc_class_ext *ext;
2843 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002844 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2845 llvm::PointerType::getUnqual(ClassTyHolder),
2846 Int8PtrTy,
2847 LongTy,
2848 LongTy,
2849 LongTy,
2850 IvarListPtrTy,
2851 MethodListPtrTy,
2852 CachePtrTy,
2853 ProtocolListPtrTy,
2854 Int8PtrTy,
2855 ClassExtensionPtrTy,
2856 NULL);
2857 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2858
2859 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2860 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2861 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2862
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002863 // struct _objc_category {
2864 // char *category_name;
2865 // char *class_name;
2866 // struct _objc_method_list *instance_method;
2867 // struct _objc_method_list *class_method;
2868 // uint32_t size; // sizeof(struct _objc_category)
2869 // struct _objc_property_list *instance_properties;// category's @property
2870 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002871 CategoryTy = llvm::StructType::get(Int8PtrTy,
2872 Int8PtrTy,
2873 MethodListPtrTy,
2874 MethodListPtrTy,
2875 ProtocolListPtrTy,
2876 IntTy,
2877 PropertyListPtrTy,
2878 NULL);
2879 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2880
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002881 // Global metadata structures
2882
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002883 // struct _objc_symtab {
2884 // long sel_ref_cnt;
2885 // SEL *refs;
2886 // short cls_def_cnt;
2887 // short cat_def_cnt;
2888 // char *defs[cls_def_cnt + cat_def_cnt];
2889 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002890 SymtabTy = llvm::StructType::get(LongTy,
2891 SelectorPtrTy,
2892 ShortTy,
2893 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002894 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002895 NULL);
2896 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2897 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2898
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002899 // struct _objc_module {
2900 // long version;
2901 // long size; // sizeof(struct _objc_module)
2902 // char *name;
2903 // struct _objc_symtab* symtab;
2904 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002905 ModuleTy =
2906 llvm::StructType::get(LongTy,
2907 LongTy,
2908 Int8PtrTy,
2909 SymtabPtrTy,
2910 NULL);
2911 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002912
Daniel Dunbar49f66022008-09-24 03:38:44 +00002913 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002914
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002915 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002916 std::vector<const llvm::Type*> Params;
2917 Params.push_back(ObjectPtrTy);
2918 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002919 MessageSendFn =
2920 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2921 Params,
2922 true),
2923 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002924
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002925 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002926 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002927 Params.push_back(ObjectPtrTy);
2928 Params.push_back(SelectorPtrTy);
2929 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002930 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2931 Params,
2932 true),
2933 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002934
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002935 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002936 Params.clear();
2937 Params.push_back(ObjectPtrTy);
2938 Params.push_back(SelectorPtrTy);
2939 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002940 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002941 MessageSendFpretFn =
2942 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2943 Params,
2944 true),
2945 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002946
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002947 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002948 Params.clear();
2949 Params.push_back(SuperPtrTy);
2950 Params.push_back(SelectorPtrTy);
2951 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002952 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2953 Params,
2954 true),
2955 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002956
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002957 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2958 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002959 Params.clear();
2960 Params.push_back(Int8PtrTy);
2961 Params.push_back(SuperPtrTy);
2962 Params.push_back(SelectorPtrTy);
2963 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002964 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2965 Params,
2966 true),
2967 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002968
2969 // There is no objc_msgSendSuper_fpret? How can that work?
2970 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002971
Anders Carlsson124526b2008-09-09 10:10:21 +00002972 // FIXME: This is the size of the setjmp buffer and should be
2973 // target specific. 18 is what's used on 32-bit X86.
2974 uint64_t SetJmpBufferSize = 18;
2975
2976 // Exceptions
2977 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002978 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002979
2980 ExceptionDataTy =
2981 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2982 SetJmpBufferSize),
2983 StackPtrTy, NULL);
2984 CGM.getModule().addTypeName("struct._objc_exception_data",
2985 ExceptionDataTy);
2986
2987 Params.clear();
2988 Params.push_back(ObjectPtrTy);
2989 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002990 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2991 Params,
2992 false),
2993 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002994
2995 Params.clear();
2996 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2997 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002998 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2999 Params,
3000 false),
3001 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003002 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003003 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3004 Params,
3005 false),
3006 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003007 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003008 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3009 Params,
3010 false),
3011 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003012
3013 Params.clear();
3014 Params.push_back(ClassPtrTy);
3015 Params.push_back(ObjectPtrTy);
3016 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003017 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3018 Params,
3019 false),
3020 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003021
3022 // synchronized APIs
3023 // void objc_sync_enter (id)
3024 Params.clear();
3025 Params.push_back(ObjectPtrTy);
3026 SyncEnterFn =
3027 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3028 Params,
3029 false),
3030 "objc_sync_enter");
3031 // void objc_sync_exit (id)
3032 SyncExitFn =
3033 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3034 Params,
3035 false),
3036 "objc_sync_exit");
3037
Anders Carlsson124526b2008-09-09 10:10:21 +00003038
3039 Params.clear();
3040 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3041 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003042 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3043 Params,
3044 false),
3045 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003046
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003047}
3048
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003049ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003050: ObjCCommonTypesHelper(cgm)
3051{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003052 // struct _method_list_t {
3053 // uint32_t entsize; // sizeof(struct _objc_method)
3054 // uint32_t method_count;
3055 // struct _objc_method method_list[method_count];
3056 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003057 MethodListnfABITy = llvm::StructType::get(IntTy,
3058 IntTy,
3059 llvm::ArrayType::get(MethodTy, 0),
3060 NULL);
3061 CGM.getModule().addTypeName("struct.__method_list_t",
3062 MethodListnfABITy);
3063 // struct method_list_t *
3064 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003065
3066 // struct _protocol_t {
3067 // id isa; // NULL
3068 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003069 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003070 // const struct method_list_t * const instance_methods;
3071 // const struct method_list_t * const class_methods;
3072 // const struct method_list_t *optionalInstanceMethods;
3073 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003074 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003075 // const uint32_t size; // sizeof(struct _protocol_t)
3076 // const uint32_t flags; // = 0
3077 // }
3078
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003079 // Holder for struct _protocol_list_t *
3080 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3081
3082 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3083 Int8PtrTy,
3084 llvm::PointerType::getUnqual(
3085 ProtocolListTyHolder),
3086 MethodListnfABIPtrTy,
3087 MethodListnfABIPtrTy,
3088 MethodListnfABIPtrTy,
3089 MethodListnfABIPtrTy,
3090 PropertyListPtrTy,
3091 IntTy,
3092 IntTy,
3093 NULL);
3094 CGM.getModule().addTypeName("struct._protocol_t",
3095 ProtocolnfABITy);
3096
Fariborz Jahanianda320092009-01-29 19:24:30 +00003097 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003098 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003099 // struct _protocol_t[protocol_count];
3100 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3102 llvm::ArrayType::get(
3103 ProtocolnfABITy, 0),
3104 NULL);
3105 CGM.getModule().addTypeName("struct._objc_protocol_list",
3106 ProtocolListnfABITy);
3107
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003108 // struct _objc_protocol_list*
3109 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003110
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003111 // FIXME! Is this doing the right thing?
3112 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3113 ProtocolListnfABIPtrTy);
3114
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003115 // struct _ivar_t {
3116 // unsigned long int *offset; // pointer to ivar offset location
3117 // char *name;
3118 // char *type;
3119 // uint32_t alignment;
3120 // uint32_t size;
3121 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003122 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3123 Int8PtrTy,
3124 Int8PtrTy,
3125 IntTy,
3126 IntTy,
3127 NULL);
3128 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3129
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003130 // struct _ivar_list_t {
3131 // uint32 entsize; // sizeof(struct _ivar_t)
3132 // uint32 count;
3133 // struct _iver_t list[count];
3134 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003135 IvarListnfABITy = llvm::StructType::get(IntTy,
3136 IntTy,
3137 llvm::ArrayType::get(
3138 IvarnfABITy, 0),
3139 NULL);
3140 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3141
3142 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003143
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003144 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003145 // uint32_t const flags;
3146 // uint32_t const instanceStart;
3147 // uint32_t const instanceSize;
3148 // uint32_t const reserved; // only when building for 64bit targets
3149 // const uint8_t * const ivarLayout;
3150 // const char *const name;
3151 // const struct _method_list_t * const baseMethods;
3152 // const struct _objc_protocol_list *const baseProtocols;
3153 // const struct _ivar_list_t *const ivars;
3154 // const uint8_t * const weakIvarLayout;
3155 // const struct _prop_list_t * const properties;
3156 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003157
3158 // FIXME. Add 'reserved' field in 64bit abi mode!
3159 ClassRonfABITy = llvm::StructType::get(IntTy,
3160 IntTy,
3161 IntTy,
3162 Int8PtrTy,
3163 Int8PtrTy,
3164 MethodListnfABIPtrTy,
3165 ProtocolListnfABIPtrTy,
3166 IvarListnfABIPtrTy,
3167 Int8PtrTy,
3168 PropertyListPtrTy,
3169 NULL);
3170 CGM.getModule().addTypeName("struct._class_ro_t",
3171 ClassRonfABITy);
3172
3173 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3174 std::vector<const llvm::Type*> Params;
3175 Params.push_back(ObjectPtrTy);
3176 Params.push_back(SelectorPtrTy);
3177 ImpnfABITy = llvm::PointerType::getUnqual(
3178 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3179
3180 // struct _class_t {
3181 // struct _class_t *isa;
3182 // struct _class_t * const superclass;
3183 // void *cache;
3184 // IMP *vtable;
3185 // struct class_ro_t *ro;
3186 // }
3187
3188 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3189 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3190 llvm::PointerType::getUnqual(ClassTyHolder),
3191 CachePtrTy,
3192 llvm::PointerType::getUnqual(ImpnfABITy),
3193 llvm::PointerType::getUnqual(
3194 ClassRonfABITy),
3195 NULL);
3196 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3197
3198 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3199 ClassnfABITy);
3200
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003201 // LLVM for struct _class_t *
3202 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3203
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003204 // struct _category_t {
3205 // const char * const name;
3206 // struct _class_t *const cls;
3207 // const struct _method_list_t * const instance_methods;
3208 // const struct _method_list_t * const class_methods;
3209 // const struct _protocol_list_t * const protocols;
3210 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003211 // }
3212 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003213 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003214 MethodListnfABIPtrTy,
3215 MethodListnfABIPtrTy,
3216 ProtocolListnfABIPtrTy,
3217 PropertyListPtrTy,
3218 NULL);
3219 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003220
3221 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003222 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3223 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003224
3225 // MessageRefTy - LLVM for:
3226 // struct _message_ref_t {
3227 // IMP messenger;
3228 // SEL name;
3229 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003230
3231 // First the clang type for struct _message_ref_t
3232 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3233 SourceLocation(),
3234 &Ctx.Idents.get("_message_ref_t"));
3235 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3236 Ctx.VoidPtrTy, 0, false));
3237 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3238 Ctx.getObjCSelType(), 0, false));
3239 RD->completeDefinition(Ctx);
3240
3241 MessageRefCTy = Ctx.getTagDeclType(RD);
3242 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3243 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003244
3245 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3246 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3247
3248 // SuperMessageRefTy - LLVM for:
3249 // struct _super_message_ref_t {
3250 // SUPER_IMP messenger;
3251 // SEL name;
3252 // };
3253 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3254 SelectorPtrTy,
3255 NULL);
3256 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3257
3258 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3259 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3260
3261 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3262 Params.clear();
3263 Params.push_back(ObjectPtrTy);
3264 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003265 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3266 Params,
3267 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003268 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003269 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003270 "objc_msgSend_fixup");
3271
3272 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3273 MessageSendFpretFixupFn =
3274 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3275 Params,
3276 true),
3277 "objc_msgSend_fpret_fixup");
3278
3279 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3280 MessageSendStretFixupFn =
3281 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3282 Params,
3283 true),
3284 "objc_msgSend_stret_fixup");
3285
3286 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3287 MessageSendIdFixupFn =
3288 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3289 Params,
3290 true),
3291 "objc_msgSendId_fixup");
3292
3293
3294 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3295 MessageSendIdStretFixupFn =
3296 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3297 Params,
3298 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003299 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003300
3301 // id objc_msgSendSuper2_fixup (struct objc_super *,
3302 // struct _super_message_ref_t*, ...)
3303 Params.clear();
3304 Params.push_back(SuperPtrTy);
3305 Params.push_back(SuperMessageRefPtrTy);
3306 MessageSendSuper2FixupFn =
3307 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3308 Params,
3309 true),
3310 "objc_msgSendSuper2_fixup");
3311
3312
3313 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3314 // struct _super_message_ref_t*, ...)
3315 MessageSendSuper2StretFixupFn =
3316 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3317 Params,
3318 true),
3319 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003320
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003321}
3322
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003323llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3324 FinishNonFragileABIModule();
3325
3326 return NULL;
3327}
3328
3329void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3330 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003331
3332 // Build list of all implemented classe addresses in array
3333 // L_OBJC_LABEL_CLASS_$.
3334 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3335 // list of 'nonlazy' implementations (defined as those with a +load{}
3336 // method!!).
3337 unsigned NumClasses = DefinedClasses.size();
3338 if (NumClasses) {
3339 std::vector<llvm::Constant*> Symbols(NumClasses);
3340 for (unsigned i=0; i<NumClasses; i++)
3341 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3342 ObjCTypes.Int8PtrTy);
3343 llvm::Constant* Init =
3344 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3345 NumClasses),
3346 Symbols);
3347
3348 llvm::GlobalVariable *GV =
3349 new llvm::GlobalVariable(Init->getType(), false,
3350 llvm::GlobalValue::InternalLinkage,
3351 Init,
3352 "\01L_OBJC_LABEL_CLASS_$",
3353 &CGM.getModule());
3354 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3355 UsedGlobals.push_back(GV);
3356 }
3357
3358 // Build list of all implemented category addresses in array
3359 // L_OBJC_LABEL_CATEGORY_$.
3360 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3361 // list of 'nonlazy' category implementations (defined as those with a +load{}
3362 // method!!).
3363 unsigned NumCategory = DefinedCategories.size();
3364 if (NumCategory) {
3365 std::vector<llvm::Constant*> Symbols(NumCategory);
3366 for (unsigned i=0; i<NumCategory; i++)
3367 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3368 ObjCTypes.Int8PtrTy);
3369 llvm::Constant* Init =
3370 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3371 NumCategory),
3372 Symbols);
3373
3374 llvm::GlobalVariable *GV =
3375 new llvm::GlobalVariable(Init->getType(), false,
3376 llvm::GlobalValue::InternalLinkage,
3377 Init,
3378 "\01L_OBJC_LABEL_CATEGORY_$",
3379 &CGM.getModule());
3380 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3381 UsedGlobals.push_back(GV);
3382 }
3383
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003384 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3385 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3386 std::vector<llvm::Constant*> Values(2);
3387 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3388 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3389 llvm::Constant* Init = llvm::ConstantArray::get(
3390 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3391 Values);
3392 llvm::GlobalVariable *IMGV =
3393 new llvm::GlobalVariable(Init->getType(), false,
3394 llvm::GlobalValue::InternalLinkage,
3395 Init,
3396 "\01L_OBJC_IMAGE_INFO",
3397 &CGM.getModule());
3398 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3399 UsedGlobals.push_back(IMGV);
3400
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003401 std::vector<llvm::Constant*> Used;
3402 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3403 e = UsedGlobals.end(); i != e; ++i) {
3404 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3405 }
3406
3407 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3408 llvm::GlobalValue *GV =
3409 new llvm::GlobalVariable(AT, false,
3410 llvm::GlobalValue::AppendingLinkage,
3411 llvm::ConstantArray::get(AT, Used),
3412 "llvm.used",
3413 &CGM.getModule());
3414
3415 GV->setSection("llvm.metadata");
3416
3417}
3418
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003419// Metadata flags
3420enum MetaDataDlags {
3421 CLS = 0x0,
3422 CLS_META = 0x1,
3423 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003424 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003425 CLS_EXCEPTION = 0x20
3426};
3427/// BuildClassRoTInitializer - generate meta-data for:
3428/// struct _class_ro_t {
3429/// uint32_t const flags;
3430/// uint32_t const instanceStart;
3431/// uint32_t const instanceSize;
3432/// uint32_t const reserved; // only when building for 64bit targets
3433/// const uint8_t * const ivarLayout;
3434/// const char *const name;
3435/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003436/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003437/// const struct _ivar_list_t *const ivars;
3438/// const uint8_t * const weakIvarLayout;
3439/// const struct _prop_list_t * const properties;
3440/// }
3441///
3442llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3443 unsigned flags,
3444 unsigned InstanceStart,
3445 unsigned InstanceSize,
3446 const ObjCImplementationDecl *ID) {
3447 std::string ClassName = ID->getNameAsString();
3448 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3449 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3450 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3451 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3452 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003453 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003454 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3455 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003456 // const struct _method_list_t * const baseMethods;
3457 std::vector<llvm::Constant*> Methods;
3458 std::string MethodListName("\01l_OBJC_$_");
3459 if (flags & CLS_META) {
3460 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3461 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3462 e = ID->classmeth_end(); i != e; ++i) {
3463 // Class methods should always be defined.
3464 Methods.push_back(GetMethodConstant(*i));
3465 }
3466 } else {
3467 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3468 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3469 e = ID->instmeth_end(); i != e; ++i) {
3470 // Instance methods should always be defined.
3471 Methods.push_back(GetMethodConstant(*i));
3472 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003473 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3474 e = ID->propimpl_end(); i != e; ++i) {
3475 ObjCPropertyImplDecl *PID = *i;
3476
3477 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3478 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3479
3480 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3481 if (llvm::Constant *C = GetMethodConstant(MD))
3482 Methods.push_back(C);
3483 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3484 if (llvm::Constant *C = GetMethodConstant(MD))
3485 Methods.push_back(C);
3486 }
3487 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003488 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003489 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003490 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003491
3492 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3493 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3494 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3495 + OID->getNameAsString(),
3496 OID->protocol_begin(),
3497 OID->protocol_end());
3498
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003499 if (flags & CLS_META)
3500 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3501 else
3502 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003503 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003504 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003505 if (flags & CLS_META)
3506 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3507 else
3508 Values[ 9] =
3509 EmitPropertyList(
3510 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3511 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003512 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3513 Values);
3514 llvm::GlobalVariable *CLASS_RO_GV =
3515 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3516 llvm::GlobalValue::InternalLinkage,
3517 Init,
3518 (flags & CLS_META) ?
3519 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3520 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3521 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003522 CLASS_RO_GV->setAlignment(
3523 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003524 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003525 UsedGlobals.push_back(CLASS_RO_GV);
3526 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003527
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003528}
3529
3530/// BuildClassMetaData - This routine defines that to-level meta-data
3531/// for the given ClassName for:
3532/// struct _class_t {
3533/// struct _class_t *isa;
3534/// struct _class_t * const superclass;
3535/// void *cache;
3536/// IMP *vtable;
3537/// struct class_ro_t *ro;
3538/// }
3539///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003540llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3541 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003542 llvm::Constant *IsAGV,
3543 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003544 llvm::Constant *ClassRoGV,
3545 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003546 std::vector<llvm::Constant*> Values(5);
3547 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003548 Values[1] = SuperClassGV
3549 ? SuperClassGV
3550 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003551 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3552 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3553 Values[4] = ClassRoGV; // &CLASS_RO_GV
3554 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3555 Values);
3556 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3557 if (GV)
3558 GV->setInitializer(Init);
3559 else
3560 GV =
3561 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3562 llvm::GlobalValue::ExternalLinkage,
3563 Init,
3564 ClassName,
3565 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003566 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003567 GV->setAlignment(
3568 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003569 if (HiddenVisibility)
3570 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003571 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003572 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003573}
3574
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003575void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3576 std::string ClassName = ID->getNameAsString();
3577 if (!ObjCEmptyCacheVar) {
3578 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3579 ObjCTypes.CachePtrTy,
3580 false,
3581 llvm::GlobalValue::ExternalLinkage,
3582 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003583 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003584 &CGM.getModule());
3585 UsedGlobals.push_back(ObjCEmptyCacheVar);
3586
3587 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3588 llvm::PointerType::getUnqual(
3589 ObjCTypes.ImpnfABITy),
3590 false,
3591 llvm::GlobalValue::ExternalLinkage,
3592 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003593 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003594 &CGM.getModule());
3595 UsedGlobals.push_back(ObjCEmptyVtableVar);
3596 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003597 assert(ID->getClassInterface() &&
3598 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003599 uint32_t InstanceStart =
3600 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3601 uint32_t InstanceSize = InstanceStart;
3602 uint32_t flags = CLS_META;
3603 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3604 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003605
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003606 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003607
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003608 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3609 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003610 flags |= OBJC2_CLS_HIDDEN;
3611 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003612 // class is root
3613 flags |= CLS_ROOT;
3614 std::string SuperClassName = ObjCClassName + ClassName;
3615 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003616 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003617 SuperClassGV =
3618 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3619 llvm::GlobalValue::ExternalLinkage,
3620 0,
3621 SuperClassName,
3622 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003623 UsedGlobals.push_back(SuperClassGV);
3624 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003625 std::string IsAClassName = ObjCMetaClassName + ClassName;
3626 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003627 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003628 IsAGV =
3629 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3630 llvm::GlobalValue::ExternalLinkage,
3631 0,
3632 IsAClassName,
3633 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003634 UsedGlobals.push_back(IsAGV);
3635 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003636 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003637 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003638 std::string RootClassName =
3639 ID->getClassInterface()->getSuperClass()->getNameAsString();
3640 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3641 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003642 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003643 SuperClassGV =
3644 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3645 llvm::GlobalValue::ExternalLinkage,
3646 0,
3647 SuperClassName,
3648 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003649 UsedGlobals.push_back(SuperClassGV);
3650 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003651 IsAGV = SuperClassGV;
3652 }
3653 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3654 InstanceStart,
3655 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003656 std::string TClassName = ObjCMetaClassName + ClassName;
3657 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003658 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3659 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003660
3661 // Metadata for the class
3662 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003663 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003664 flags |= OBJC2_CLS_HIDDEN;
3665 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003666 flags |= CLS_ROOT;
3667 SuperClassGV = 0;
3668 }
3669 else {
3670 // Has a root. Current class is not a root.
3671 std::string RootClassName =
3672 ID->getClassInterface()->getSuperClass()->getNameAsString();
3673 std::string SuperClassName = ObjCClassName + RootClassName;
3674 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003675 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003676 SuperClassGV =
3677 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3678 llvm::GlobalValue::ExternalLinkage,
3679 0,
3680 SuperClassName,
3681 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003682 UsedGlobals.push_back(SuperClassGV);
3683 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003684 }
3685
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003686 InstanceStart = InstanceSize = 0;
3687 if (ObjCInterfaceDecl *OID =
3688 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3689 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003690 const llvm::Type *InterfaceTy =
3691 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3692 const llvm::StructLayout *Layout =
3693 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003694
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003695 RecordDecl::field_iterator firstField, lastField;
3696 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003697
3698 for (RecordDecl::field_iterator e = RD->field_end(),
3699 ifield = firstField; ifield != e; ++ifield)
3700 lastField = ifield;
3701
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003702 if (lastField != RD->field_end()) {
3703 FieldDecl *Field = *lastField;
3704 const llvm::Type *FieldTy =
3705 CGM.getTypes().ConvertTypeForMem(Field->getType());
3706 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3707 InstanceSize = Layout->getElementOffset(
3708 CGM.getTypes().getLLVMFieldNo(Field)) +
3709 Size;
3710 if (firstField == RD->field_end())
3711 InstanceStart = InstanceSize;
3712 else
3713 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3714 getLLVMFieldNo(*firstField));
3715 }
3716 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003717 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003718 InstanceStart,
3719 InstanceSize,
3720 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003721
3722 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003723 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003724 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3725 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003726 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003727}
3728
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003729/// GenerateProtocolRef - This routine is called to generate code for
3730/// a protocol reference expression; as in:
3731/// @code
3732/// @protocol(Proto1);
3733/// @endcode
3734/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3735/// which will hold address of the protocol meta-data.
3736///
3737llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3738 const ObjCProtocolDecl *PD) {
3739
3740 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3741 ObjCTypes.ExternalProtocolPtrTy);
3742
3743 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3744 ProtocolName += PD->getNameAsCString();
3745
3746 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3747 if (PTGV)
3748 return Builder.CreateLoad(PTGV, false, "tmp");
3749 PTGV = new llvm::GlobalVariable(
3750 Init->getType(), false,
3751 llvm::GlobalValue::WeakLinkage,
3752 Init,
3753 ProtocolName,
3754 &CGM.getModule());
3755 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3756 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3757 UsedGlobals.push_back(PTGV);
3758 return Builder.CreateLoad(PTGV, false, "tmp");
3759}
3760
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003761/// GenerateCategory - Build metadata for a category implementation.
3762/// struct _category_t {
3763/// const char * const name;
3764/// struct _class_t *const cls;
3765/// const struct _method_list_t * const instance_methods;
3766/// const struct _method_list_t * const class_methods;
3767/// const struct _protocol_list_t * const protocols;
3768/// const struct _prop_list_t * const properties;
3769/// }
3770///
3771void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3772{
3773 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003774 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3775 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003776 "_$_" + OCD->getNameAsString());
3777 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3778
3779 std::vector<llvm::Constant*> Values(6);
3780 Values[0] = GetClassName(OCD->getIdentifier());
3781 // meta-class entry symbol
3782 llvm::GlobalVariable *ClassGV =
3783 CGM.getModule().getGlobalVariable(ExtClassName);
3784 if (!ClassGV)
3785 ClassGV =
3786 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3787 llvm::GlobalValue::ExternalLinkage,
3788 0,
3789 ExtClassName,
3790 &CGM.getModule());
3791 UsedGlobals.push_back(ClassGV);
3792 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003793 std::vector<llvm::Constant*> Methods;
3794 std::string MethodListName(Prefix);
3795 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3796 "_$_" + OCD->getNameAsString();
3797
3798 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3799 e = OCD->instmeth_end(); i != e; ++i) {
3800 // Instance methods should always be defined.
3801 Methods.push_back(GetMethodConstant(*i));
3802 }
3803
3804 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003805 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003806 Methods);
3807
3808 MethodListName = Prefix;
3809 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3810 OCD->getNameAsString();
3811 Methods.clear();
3812 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3813 e = OCD->classmeth_end(); i != e; ++i) {
3814 // Class methods should always be defined.
3815 Methods.push_back(GetMethodConstant(*i));
3816 }
3817
3818 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003819 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003820 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003821 const ObjCCategoryDecl *Category =
3822 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003823 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3824 + Interface->getNameAsString() + "_$_"
3825 + Category->getNameAsString(),
3826 Category->protocol_begin(),
3827 Category->protocol_end());
3828
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003829 std::string ExtName(Interface->getNameAsString() + "_$_" +
3830 OCD->getNameAsString());
3831 Values[5] =
3832 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3833 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003834 llvm::Constant *Init =
3835 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3836 Values);
3837 llvm::GlobalVariable *GCATV
3838 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3839 false,
3840 llvm::GlobalValue::InternalLinkage,
3841 Init,
3842 ExtCatName,
3843 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003844 GCATV->setAlignment(
3845 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003846 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003847 UsedGlobals.push_back(GCATV);
3848 DefinedCategories.push_back(GCATV);
3849}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003850
3851/// GetMethodConstant - Return a struct objc_method constant for the
3852/// given method if it has been defined. The result is null if the
3853/// method has not been defined. The return value has type MethodPtrTy.
3854llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3855 const ObjCMethodDecl *MD) {
3856 // FIXME: Use DenseMap::lookup
3857 llvm::Function *Fn = MethodDefinitions[MD];
3858 if (!Fn)
3859 return 0;
3860
3861 std::vector<llvm::Constant*> Method(3);
3862 Method[0] =
3863 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3864 ObjCTypes.SelectorPtrTy);
3865 Method[1] = GetMethodVarType(MD);
3866 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3867 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3868}
3869
3870/// EmitMethodList - Build meta-data for method declarations
3871/// struct _method_list_t {
3872/// uint32_t entsize; // sizeof(struct _objc_method)
3873/// uint32_t method_count;
3874/// struct _objc_method method_list[method_count];
3875/// }
3876///
3877llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3878 const std::string &Name,
3879 const char *Section,
3880 const ConstantVector &Methods) {
3881 // Return null for empty list.
3882 if (Methods.empty())
3883 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3884
3885 std::vector<llvm::Constant*> Values(3);
3886 // sizeof(struct _objc_method)
3887 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3888 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3889 // method_count
3890 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3891 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3892 Methods.size());
3893 Values[2] = llvm::ConstantArray::get(AT, Methods);
3894 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3895
3896 llvm::GlobalVariable *GV =
3897 new llvm::GlobalVariable(Init->getType(), false,
3898 llvm::GlobalValue::InternalLinkage,
3899 Init,
3900 Name,
3901 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003902 GV->setAlignment(
3903 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003904 GV->setSection(Section);
3905 UsedGlobals.push_back(GV);
3906 return llvm::ConstantExpr::getBitCast(GV,
3907 ObjCTypes.MethodListnfABIPtrTy);
3908}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003909
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003910llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3911 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003912 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003913 unsigned long int Offset) {
3914
3915 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003916 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003917 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003918
3919 llvm::GlobalVariable *IvarOffsetGV =
3920 CGM.getModule().getGlobalVariable(ExternalName);
3921 if (IvarOffsetGV) {
3922 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003923 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003924 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003925 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003926 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003927 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003928 return IvarOffsetGV;
3929 }
3930
3931 IvarOffsetGV =
3932 new llvm::GlobalVariable(Init->getType(),
3933 false,
3934 llvm::GlobalValue::ExternalLinkage,
3935 Init,
3936 ExternalName,
3937 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003938 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003939 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003940 // @private and @package have hidden visibility.
3941 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3942 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3943 if (!globalVisibility)
3944 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003945 else
3946 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3947 if (IsClassHidden(OID))
3948 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3949
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003950 IvarOffsetGV->setSection("__DATA, __objc_const");
3951 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003952 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003953}
3954
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003955/// EmitIvarList - Emit the ivar list for the given
3956/// implementation. If ForClass is true the list of class ivars
3957/// (i.e. metaclass ivars) is emitted, otherwise the list of
3958/// interface ivars will be emitted. The return value has type
3959/// IvarListnfABIPtrTy.
3960/// struct _ivar_t {
3961/// unsigned long int *offset; // pointer to ivar offset location
3962/// char *name;
3963/// char *type;
3964/// uint32_t alignment;
3965/// uint32_t size;
3966/// }
3967/// struct _ivar_list_t {
3968/// uint32 entsize; // sizeof(struct _ivar_t)
3969/// uint32 count;
3970/// struct _iver_t list[count];
3971/// }
3972///
3973llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3974 const ObjCImplementationDecl *ID) {
3975
3976 std::vector<llvm::Constant*> Ivars, Ivar(5);
3977
3978 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3979 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3980
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003981 // FIXME. Consolidate this with similar code in GenerateClass.
3982 const llvm::Type *InterfaceTy =
3983 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3984 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003985 const llvm::StructLayout *Layout =
3986 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003987
3988 RecordDecl::field_iterator i,p;
3989 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003990 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3991
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003992 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003993 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003994 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3995 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003996 const ObjCIvarDecl *ivarDecl = *I++;
3997 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003998 if (Field->getIdentifier())
3999 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4000 else
4001 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4002 std::string TypeStr;
4003 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4004 Ivar[2] = GetMethodVarType(TypeStr);
4005 const llvm::Type *FieldTy =
4006 CGM.getTypes().ConvertTypeForMem(Field->getType());
4007 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4008 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4009 Field->getType().getTypePtr()) >> 3;
4010 Align = llvm::Log2_32(Align);
4011 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004012 // NOTE. Size of a bitfield does not match gcc's, because of the way
4013 // bitfields are treated special in each. But I am told that 'size'
4014 // for bitfield ivars is ignored by the runtime so it does not matter.
4015 // (even if it matters, some day, there is enough info. to get the bitfield
4016 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004017 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4018 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4019 }
4020 // Return null for empty list.
4021 if (Ivars.empty())
4022 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4023 std::vector<llvm::Constant*> Values(3);
4024 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4025 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4026 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4027 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4028 Ivars.size());
4029 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4030 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4031 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4032 llvm::GlobalVariable *GV =
4033 new llvm::GlobalVariable(Init->getType(), false,
4034 llvm::GlobalValue::InternalLinkage,
4035 Init,
4036 Prefix + OID->getNameAsString(),
4037 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004038 GV->setAlignment(
4039 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004040 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004041
4042 UsedGlobals.push_back(GV);
4043 return llvm::ConstantExpr::getBitCast(GV,
4044 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004045}
4046
4047llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4048 const ObjCProtocolDecl *PD) {
4049 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4050
4051 if (!Entry) {
4052 // We use the initializer as a marker of whether this is a forward
4053 // reference or not. At module finalization we add the empty
4054 // contents for protocols which were referenced but never defined.
4055 Entry =
4056 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4057 llvm::GlobalValue::ExternalLinkage,
4058 0,
4059 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4060 &CGM.getModule());
4061 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4062 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004063 }
4064
4065 return Entry;
4066}
4067
4068/// GetOrEmitProtocol - Generate the protocol meta-data:
4069/// @code
4070/// struct _protocol_t {
4071/// id isa; // NULL
4072/// const char * const protocol_name;
4073/// const struct _protocol_list_t * protocol_list; // super protocols
4074/// const struct method_list_t * const instance_methods;
4075/// const struct method_list_t * const class_methods;
4076/// const struct method_list_t *optionalInstanceMethods;
4077/// const struct method_list_t *optionalClassMethods;
4078/// const struct _prop_list_t * properties;
4079/// const uint32_t size; // sizeof(struct _protocol_t)
4080/// const uint32_t flags; // = 0
4081/// }
4082/// @endcode
4083///
4084
4085llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4086 const ObjCProtocolDecl *PD) {
4087 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4088
4089 // Early exit if a defining object has already been generated.
4090 if (Entry && Entry->hasInitializer())
4091 return Entry;
4092
4093 const char *ProtocolName = PD->getNameAsCString();
4094
4095 // Construct method lists.
4096 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4097 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4098 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4099 e = PD->instmeth_end(); i != e; ++i) {
4100 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004101 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004102 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4103 OptInstanceMethods.push_back(C);
4104 } else {
4105 InstanceMethods.push_back(C);
4106 }
4107 }
4108
4109 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4110 e = PD->classmeth_end(); i != e; ++i) {
4111 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004112 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004113 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4114 OptClassMethods.push_back(C);
4115 } else {
4116 ClassMethods.push_back(C);
4117 }
4118 }
4119
4120 std::vector<llvm::Constant*> Values(10);
4121 // isa is NULL
4122 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4123 Values[1] = GetClassName(PD->getIdentifier());
4124 Values[2] = EmitProtocolList(
4125 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4126 PD->protocol_begin(),
4127 PD->protocol_end());
4128
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004129 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004130 + PD->getNameAsString(),
4131 "__DATA, __objc_const",
4132 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004133 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004134 + PD->getNameAsString(),
4135 "__DATA, __objc_const",
4136 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004137 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004138 + PD->getNameAsString(),
4139 "__DATA, __objc_const",
4140 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004141 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004142 + PD->getNameAsString(),
4143 "__DATA, __objc_const",
4144 OptClassMethods);
4145 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4146 0, PD, ObjCTypes);
4147 uint32_t Size =
4148 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4149 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4150 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4151 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4152 Values);
4153
4154 if (Entry) {
4155 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004156 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004157 Entry->setInitializer(Init);
4158 } else {
4159 Entry =
4160 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004161 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004162 Init,
4163 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4164 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004165 Entry->setAlignment(
4166 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004167 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004168 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004169 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4170
4171 // Use this protocol meta-data to build protocol list table in section
4172 // __DATA, __objc_protolist
4173 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4174 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4175 ptype, false,
4176 llvm::GlobalValue::WeakLinkage,
4177 Entry,
4178 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4179 +ProtocolName,
4180 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004181 PTGV->setAlignment(
4182 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004183 PTGV->setSection("__DATA, __objc_protolist");
4184 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4185 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004186 return Entry;
4187}
4188
4189/// EmitProtocolList - Generate protocol list meta-data:
4190/// @code
4191/// struct _protocol_list_t {
4192/// long protocol_count; // Note, this is 32/64 bit
4193/// struct _protocol_t[protocol_count];
4194/// }
4195/// @endcode
4196///
4197llvm::Constant *
4198CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4199 ObjCProtocolDecl::protocol_iterator begin,
4200 ObjCProtocolDecl::protocol_iterator end) {
4201 std::vector<llvm::Constant*> ProtocolRefs;
4202
4203 for (; begin != end; ++begin)
4204 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4205
4206 // Just return null for empty protocol lists
4207 if (ProtocolRefs.empty())
4208 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4209
4210 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4211 if (GV)
4212 return GV;
4213 // This list is null terminated.
4214 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4215 ObjCTypes.ProtocolListnfABIPtrTy));
4216
4217 std::vector<llvm::Constant*> Values(2);
4218 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4219 Values[1] =
4220 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4221 ProtocolRefs.size()),
4222 ProtocolRefs);
4223
4224 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4225 GV = new llvm::GlobalVariable(Init->getType(), false,
4226 llvm::GlobalValue::InternalLinkage,
4227 Init,
4228 Name,
4229 &CGM.getModule());
4230 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004231 GV->setAlignment(
4232 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004233 UsedGlobals.push_back(GV);
4234 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4235}
4236
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004237/// GetMethodDescriptionConstant - This routine build following meta-data:
4238/// struct _objc_method {
4239/// SEL _cmd;
4240/// char *method_type;
4241/// char *_imp;
4242/// }
4243
4244llvm::Constant *
4245CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4246 std::vector<llvm::Constant*> Desc(3);
4247 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4248 ObjCTypes.SelectorPtrTy);
4249 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004250 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004251 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4252 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4253}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004254
4255/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4256/// This code gen. amounts to generating code for:
4257/// @code
4258/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4259/// @encode
4260///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004261LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004262 CodeGen::CodeGenFunction &CGF,
4263 QualType ObjectTy,
4264 llvm::Value *BaseValue,
4265 const ObjCIvarDecl *Ivar,
4266 const FieldDecl *Field,
4267 unsigned CVRQualifiers) {
4268 assert(ObjectTy->isObjCInterfaceType() &&
4269 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4270 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4271 // NOTE. This name must match one in EmitIvarOffsetVar.
4272 // FIXME. Consolidate into one naming routine.
4273 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
4274 + Ivar->getNameAsString());
4275
4276 llvm::GlobalVariable *IvarOffsetGV =
4277 CGM.getModule().getGlobalVariable(ExternalName);
4278 if (!IvarOffsetGV)
4279 IvarOffsetGV =
4280 new llvm::GlobalVariable(ObjCTypes.LongTy,
4281 false,
4282 llvm::GlobalValue::ExternalLinkage,
4283 0,
4284 ExternalName,
4285 &CGM.getModule());
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004286
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004287 // (char *) BaseValue
4288 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4289 ObjCTypes.Int8PtrTy);
4290 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4291 // (char*)BaseValue + Offset_symbol
4292 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4293 // (type *)((char*)BaseValue + Offset_symbol)
4294 const llvm::Type *IvarTy =
4295 CGM.getTypes().ConvertType(Ivar->getType());
4296 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4297 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004298
4299 if (Ivar->isBitField())
4300 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4301 CVRQualifiers);
4302
4303 LValue LV = LValue::MakeAddr(V,
4304 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4305 LValue::SetObjCIvar(LV, true);
4306 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004307}
4308
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004309llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4310 CodeGen::CodeGenFunction &CGF,
4311 ObjCInterfaceDecl *Interface,
4312 const ObjCIvarDecl *Ivar) {
4313 return 0;
4314}
4315
Fariborz Jahanian46551122009-02-04 00:22:57 +00004316CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4317 CodeGen::CodeGenFunction &CGF,
4318 QualType ResultType,
4319 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004320 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004321 QualType Arg0Ty,
4322 bool IsSuper,
4323 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004324 // FIXME. Even though IsSuper is passes. This function doese not
4325 // handle calls to 'super' receivers.
4326 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004327 llvm::Value *Arg0 = Receiver;
4328 if (!IsSuper)
4329 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004330
4331 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004332 // FIXME. This is too much work to get the ABI-specific result type
4333 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004334 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4335 llvm::SmallVector<QualType, 16>());
4336 llvm::Constant *Fn;
4337 std::string Name("\01l_");
4338 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004339#if 0
4340 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004341 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4342 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4343 // FIXME. Is there a better way of getting these names.
4344 // They are available in RuntimeFunctions vector pair.
4345 Name += "objc_msgSendId_stret_fixup";
4346 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004347 else
4348#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004349 if (IsSuper) {
4350 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4351 Name += "objc_msgSendSuper2_stret_fixup";
4352 }
4353 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004354 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004355 Fn = ObjCTypes.MessageSendStretFixupFn;
4356 Name += "objc_msgSend_stret_fixup";
4357 }
4358 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004359 else if (ResultType->isFloatingType() &&
4360 // Selection of frret API only happens in 32bit nonfragile ABI.
4361 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004362 Fn = ObjCTypes.MessageSendFpretFixupFn;
4363 Name += "objc_msgSend_fpret_fixup";
4364 }
4365 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004366#if 0
4367// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004368 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4369 Fn = ObjCTypes.MessageSendIdFixupFn;
4370 Name += "objc_msgSendId_fixup";
4371 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004372 else
4373#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004374 if (IsSuper) {
4375 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4376 Name += "objc_msgSendSuper2_fixup";
4377 }
4378 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004379 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004380 Fn = ObjCTypes.MessageSendFixupFn;
4381 Name += "objc_msgSend_fixup";
4382 }
4383 }
4384 Name += '_';
4385 std::string SelName(Sel.getAsString());
4386 // Replace all ':' in selector name with '_' ouch!
4387 for(unsigned i = 0; i < SelName.size(); i++)
4388 if (SelName[i] == ':')
4389 SelName[i] = '_';
4390 Name += SelName;
4391 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4392 if (!GV) {
4393 // Build messafe ref table entry.
4394 std::vector<llvm::Constant*> Values(2);
4395 Values[0] = Fn;
4396 Values[1] = GetMethodVarName(Sel);
4397 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4398 GV = new llvm::GlobalVariable(Init->getType(), false,
4399 llvm::GlobalValue::WeakLinkage,
4400 Init,
4401 Name,
4402 &CGM.getModule());
4403 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4404 GV->setAlignment(
4405 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4406 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4407 UsedGlobals.push_back(GV);
4408 }
4409 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004410
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004411 CallArgList ActualArgs;
4412 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4413 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4414 ObjCTypes.MessageRefCPtrTy));
4415 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004416 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4417 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4418 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004419 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, false);
4420 Callee = CGF.Builder.CreateBitCast(Callee,
4421 llvm::PointerType::getUnqual(FTy));
4422 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004423}
4424
4425/// Generate code for a message send expression in the nonfragile abi.
4426CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4427 CodeGen::CodeGenFunction &CGF,
4428 QualType ResultType,
4429 Selector Sel,
4430 llvm::Value *Receiver,
4431 bool IsClassMessage,
4432 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004433 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004434 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004435 false, CallArgs);
4436}
4437
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004438llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004439 const ObjCInterfaceDecl *ID,
4440 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004441
4442 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4443
4444 if (!Entry) {
4445 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4446 llvm::GlobalVariable *ClassGV =
4447 CGM.getModule().getGlobalVariable(ClassName);
4448 if (!ClassGV) {
4449 ClassGV =
4450 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4451 llvm::GlobalValue::ExternalLinkage,
4452 0,
4453 ClassName,
4454 &CGM.getModule());
4455 UsedGlobals.push_back(ClassGV);
4456 }
4457 Entry =
4458 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4459 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004460 ClassGV,
4461 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4462 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004463 &CGM.getModule());
4464 Entry->setAlignment(
4465 CGM.getTargetData().getPrefTypeAlignment(
4466 ObjCTypes.ClassnfABIPtrTy));
4467
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004468 if (IsSuper)
4469 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4470 else
4471 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004472 UsedGlobals.push_back(Entry);
4473 }
4474
4475 return Builder.CreateLoad(Entry, false, "tmp");
4476}
4477
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004478/// EmitMetaClassRef - Return a Value * of the address of _class_t
4479/// meta-data
4480///
4481llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4482 const ObjCInterfaceDecl *ID) {
4483 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4484 if (Entry)
4485 return Builder.CreateLoad(Entry, false, "tmp");
4486
4487 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4488 llvm::GlobalVariable *MetaClassGV =
4489 CGM.getModule().getGlobalVariable(MetaClassName);
4490 if (!MetaClassGV) {
4491 MetaClassGV =
4492 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4493 llvm::GlobalValue::ExternalLinkage,
4494 0,
4495 MetaClassName,
4496 &CGM.getModule());
4497 UsedGlobals.push_back(MetaClassGV);
4498 }
4499
4500 Entry =
4501 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4502 llvm::GlobalValue::InternalLinkage,
4503 MetaClassGV,
4504 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4505 &CGM.getModule());
4506 Entry->setAlignment(
4507 CGM.getTargetData().getPrefTypeAlignment(
4508 ObjCTypes.ClassnfABIPtrTy));
4509
4510 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4511 UsedGlobals.push_back(Entry);
4512
4513 return Builder.CreateLoad(Entry, false, "tmp");
4514}
4515
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004516/// GetClass - Return a reference to the class for the given interface
4517/// decl.
4518llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4519 const ObjCInterfaceDecl *ID) {
4520 return EmitClassRef(Builder, ID);
4521}
4522
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004523/// Generates a message send where the super is the receiver. This is
4524/// a message send to self with special delivery semantics indicating
4525/// which class's method should be called.
4526CodeGen::RValue
4527CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4528 QualType ResultType,
4529 Selector Sel,
4530 const ObjCInterfaceDecl *Class,
4531 llvm::Value *Receiver,
4532 bool IsClassMessage,
4533 const CodeGen::CallArgList &CallArgs) {
4534 // ...
4535 // Create and init a super structure; this is a (receiver, class)
4536 // pair we will pass to objc_msgSendSuper.
4537 llvm::Value *ObjCSuper =
4538 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4539
4540 llvm::Value *ReceiverAsObject =
4541 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4542 CGF.Builder.CreateStore(ReceiverAsObject,
4543 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4544
4545 // If this is a class message the metaclass is passed as the target.
4546 llvm::Value *Target =
4547 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4548 : EmitClassRef(CGF.Builder, Class, true);
4549
4550 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4551 // and ObjCTypes types.
4552 const llvm::Type *ClassTy =
4553 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4554 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4555 CGF.Builder.CreateStore(Target,
4556 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4557
4558 return EmitMessageSend(CGF, ResultType, Sel,
4559 ObjCSuper, ObjCTypes.SuperPtrCTy,
4560 true, CallArgs);
4561}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004562/* *** */
4563
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004564CodeGen::CGObjCRuntime *
4565CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004566 return new CGObjCMac(CGM);
4567}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004568
4569CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004570CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004571 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004572}