blob: cce12a4283fd7d3835e4acb78be19f70d1d9607e [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);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000593};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000594
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000595class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000596private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000597 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000598 llvm::GlobalVariable* ObjCEmptyCacheVar;
599 llvm::GlobalVariable* ObjCEmptyVtableVar;
600
601 /// FinishNonFragileABIModule - Write out global data structures at the end of
602 /// processing a translation unit.
603 void FinishNonFragileABIModule();
604
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000605 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
606 unsigned InstanceStart,
607 unsigned InstanceSize,
608 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000609 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
610 llvm::Constant *IsAGV,
611 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000612 llvm::Constant *ClassRoGV,
613 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000614
615 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
616
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000617 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
618
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000619 /// EmitMethodList - Emit the method list for the given
620 /// implementation. The return value has type MethodListnfABITy.
621 llvm::Constant *EmitMethodList(const std::string &Name,
622 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000623 const ConstantVector &Methods);
624 /// EmitIvarList - Emit the ivar list for the given
625 /// implementation. If ForClass is true the list of class ivars
626 /// (i.e. metaclass ivars) is emitted, otherwise the list of
627 /// interface ivars will be emitted. The return value has type
628 /// IvarListnfABIPtrTy.
629 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000630
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000631 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000632 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000633 unsigned long int offset);
634
Fariborz Jahanianda320092009-01-29 19:24:30 +0000635 /// GetOrEmitProtocol - Get the protocol object for the given
636 /// declaration, emitting it if necessary. The return value has type
637 /// ProtocolPtrTy.
638 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
639
640 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
641 /// object for the given declaration, emitting it if needed. These
642 /// forward references will be filled in with empty bodies if no
643 /// definition is seen. The return value has type ProtocolPtrTy.
644 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
645
646 /// EmitProtocolList - Generate the list of referenced
647 /// protocols. The return value has type ProtocolListPtrTy.
648 llvm::Constant *EmitProtocolList(const std::string &Name,
649 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000650 ObjCProtocolDecl::protocol_iterator end);
651
652 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
653 QualType ResultType,
654 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000655 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000656 QualType Arg0Ty,
657 bool IsSuper,
658 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000659
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000660public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000661 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000662 // FIXME. All stubs for now!
663 virtual llvm::Function *ModuleInitFunction();
664
665 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
666 QualType ResultType,
667 Selector Sel,
668 llvm::Value *Receiver,
669 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000670 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000671
672 virtual CodeGen::RValue
673 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
674 QualType ResultType,
675 Selector Sel,
676 const ObjCInterfaceDecl *Class,
677 llvm::Value *Receiver,
678 bool IsClassMessage,
679 const CallArgList &CallArgs){ return RValue::get(0);}
680
681 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
682 const ObjCInterfaceDecl *ID){ return 0; }
683
684 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
685 { return 0; }
686
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000687 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000688
689 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000690 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000691 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000692
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000693 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
694 virtual llvm::Function *GetPropertySetFunction()
695 { return 0; }
696 virtual llvm::Function *EnumerationMutationFunction()
697 { return 0; }
698
699 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
700 const Stmt &S)
701 { return; }
702 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
703 const ObjCAtThrowStmt &S)
704 { return; }
705 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
706 llvm::Value *AddrWeakObj)
707 { return 0; }
708 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
709 llvm::Value *src, llvm::Value *dst)
710 { return; }
711 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
712 llvm::Value *src, llvm::Value *dest)
713 { return; }
714 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
715 llvm::Value *src, llvm::Value *dest)
716 { return; }
717 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
718 llvm::Value *src, llvm::Value *dest)
719 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000720 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
721 QualType ObjectTy,
722 llvm::Value *BaseValue,
723 const ObjCIvarDecl *Ivar,
724 const FieldDecl *Field,
725 unsigned CVRQualifiers);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000726};
727
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000728} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000729
730/* *** Helper Functions *** */
731
732/// getConstantGEP() - Help routine to construct simple GEPs.
733static llvm::Constant *getConstantGEP(llvm::Constant *C,
734 unsigned idx0,
735 unsigned idx1) {
736 llvm::Value *Idxs[] = {
737 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
738 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
739 };
740 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
741}
742
743/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000744
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000745CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
746 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000747{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000748 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000749 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000750}
751
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000752/// GetClass - Return a reference to the class for the given interface
753/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000754llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000755 const ObjCInterfaceDecl *ID) {
756 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000757}
758
759/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000760llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000761 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000762}
763
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000764/// Generate a constant CFString object.
765/*
766 struct __builtin_CFString {
767 const int *isa; // point to __CFConstantStringClassReference
768 int flags;
769 const char *str;
770 long length;
771 };
772*/
773
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000774llvm::Constant *CGObjCCommonMac::GenerateConstantString(
775 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000776 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000777}
778
779/// Generates a message send where the super is the receiver. This is
780/// a message send to self with special delivery semantics indicating
781/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000782CodeGen::RValue
783CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000784 QualType ResultType,
785 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000786 const ObjCInterfaceDecl *Class,
787 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000788 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000789 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000790 // Create and init a super structure; this is a (receiver, class)
791 // pair we will pass to objc_msgSendSuper.
792 llvm::Value *ObjCSuper =
793 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
794 llvm::Value *ReceiverAsObject =
795 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
796 CGF.Builder.CreateStore(ReceiverAsObject,
797 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000798
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000799 // If this is a class message the metaclass is passed as the target.
800 llvm::Value *Target;
801 if (IsClassMessage) {
802 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
803 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
804 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
805 Target = Super;
806 } else {
807 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
808 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000809 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
810 // and ObjCTypes types.
811 const llvm::Type *ClassTy =
812 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000813 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000814 CGF.Builder.CreateStore(Target,
815 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
816
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000817 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000818 ObjCSuper, ObjCTypes.SuperPtrCTy,
819 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000820}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000821
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000822/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000823CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000824 QualType ResultType,
825 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000826 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000827 bool IsClassMessage,
828 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000829 llvm::Value *Arg0 =
830 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000831 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000832 Arg0, CGF.getContext().getObjCIdType(),
833 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000834}
835
836CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000837 QualType ResultType,
838 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000839 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000840 QualType Arg0Ty,
841 bool IsSuper,
842 const CallArgList &CallArgs) {
843 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000844 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
845 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
846 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000847 CGF.getContext().getObjCSelType()));
848 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000849
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000850 CodeGenTypes &Types = CGM.getTypes();
851 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
852 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000853
854 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000855 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000856 Fn = ObjCTypes.getSendStretFn(IsSuper);
857 } else if (ResultType->isFloatingType()) {
858 // FIXME: Sadly, this is wrong. This actually depends on the
859 // architecture. This happens to be right for x86-32 though.
860 Fn = ObjCTypes.getSendFpretFn(IsSuper);
861 } else {
862 Fn = ObjCTypes.getSendFn(IsSuper);
863 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000864 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000865 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000866}
867
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000868llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000869 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000870 // FIXME: I don't understand why gcc generates this, or where it is
871 // resolved. Investigate. Its also wasteful to look this up over and
872 // over.
873 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
874
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000875 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
876 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000877}
878
Fariborz Jahanianda320092009-01-29 19:24:30 +0000879void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000880 // FIXME: We shouldn't need this, the protocol decl should contain
881 // enough information to tell us whether this was a declaration or a
882 // definition.
883 DefinedProtocols.insert(PD->getIdentifier());
884
885 // If we have generated a forward reference to this protocol, emit
886 // it now. Otherwise do nothing, the protocol objects are lazily
887 // emitted.
888 if (Protocols.count(PD->getIdentifier()))
889 GetOrEmitProtocol(PD);
890}
891
Fariborz Jahanianda320092009-01-29 19:24:30 +0000892llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000893 if (DefinedProtocols.count(PD->getIdentifier()))
894 return GetOrEmitProtocol(PD);
895 return GetOrEmitProtocolRef(PD);
896}
897
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000898/*
899 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
900 struct _objc_protocol {
901 struct _objc_protocol_extension *isa;
902 char *protocol_name;
903 struct _objc_protocol_list *protocol_list;
904 struct _objc__method_prototype_list *instance_methods;
905 struct _objc__method_prototype_list *class_methods
906 };
907
908 See EmitProtocolExtension().
909*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000910llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
911 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
912
913 // Early exit if a defining object has already been generated.
914 if (Entry && Entry->hasInitializer())
915 return Entry;
916
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000917 // FIXME: I don't understand why gcc generates this, or where it is
918 // resolved. Investigate. Its also wasteful to look this up over and
919 // over.
920 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
921
Chris Lattner8ec03f52008-11-24 03:54:41 +0000922 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000923
924 // Construct method lists.
925 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
926 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
927 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
928 e = PD->instmeth_end(); i != e; ++i) {
929 ObjCMethodDecl *MD = *i;
930 llvm::Constant *C = GetMethodDescriptionConstant(MD);
931 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
932 OptInstanceMethods.push_back(C);
933 } else {
934 InstanceMethods.push_back(C);
935 }
936 }
937
938 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
939 e = PD->classmeth_end(); i != e; ++i) {
940 ObjCMethodDecl *MD = *i;
941 llvm::Constant *C = GetMethodDescriptionConstant(MD);
942 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
943 OptClassMethods.push_back(C);
944 } else {
945 ClassMethods.push_back(C);
946 }
947 }
948
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000949 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000950 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000951 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000952 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000953 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000954 PD->protocol_begin(),
955 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000956 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000957 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
958 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
960 InstanceMethods);
961 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000962 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
963 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
965 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000966 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
967 Values);
968
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000970 // Already created, fix the linkage and update the initializer.
971 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000972 Entry->setInitializer(Init);
973 } else {
974 Entry =
975 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
976 llvm::GlobalValue::InternalLinkage,
977 Init,
978 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
979 &CGM.getModule());
980 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
981 UsedGlobals.push_back(Entry);
982 // FIXME: Is this necessary? Why only for protocol?
983 Entry->setAlignment(4);
984 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000985
986 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987}
988
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000989llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
991
992 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000993 // We use the initializer as a marker of whether this is a forward
994 // reference or not. At module finalization we add the empty
995 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000996 Entry =
997 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000998 llvm::GlobalValue::ExternalLinkage,
999 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001000 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001 &CGM.getModule());
1002 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1003 UsedGlobals.push_back(Entry);
1004 // FIXME: Is this necessary? Why only for protocol?
1005 Entry->setAlignment(4);
1006 }
1007
1008 return Entry;
1009}
1010
1011/*
1012 struct _objc_protocol_extension {
1013 uint32_t size;
1014 struct objc_method_description_list *optional_instance_methods;
1015 struct objc_method_description_list *optional_class_methods;
1016 struct objc_property_list *instance_properties;
1017 };
1018*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001019llvm::Constant *
1020CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1021 const ConstantVector &OptInstanceMethods,
1022 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001024 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001025 std::vector<llvm::Constant*> Values(4);
1026 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001027 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001028 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1029 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001030 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1031 OptInstanceMethods);
1032 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001033 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1034 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001035 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1036 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001037 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1038 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001039 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001041 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001042 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1043 Values[3]->isNullValue())
1044 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1045
1046 llvm::Constant *Init =
1047 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1048 llvm::GlobalVariable *GV =
1049 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1050 llvm::GlobalValue::InternalLinkage,
1051 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001052 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001053 &CGM.getModule());
1054 // No special section, but goes in llvm.used
1055 UsedGlobals.push_back(GV);
1056
1057 return GV;
1058}
1059
1060/*
1061 struct objc_protocol_list {
1062 struct objc_protocol_list *next;
1063 long count;
1064 Protocol *list[];
1065 };
1066*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001067llvm::Constant *
1068CGObjCMac::EmitProtocolList(const std::string &Name,
1069 ObjCProtocolDecl::protocol_iterator begin,
1070 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001071 std::vector<llvm::Constant*> ProtocolRefs;
1072
Daniel Dunbardbc933702008-08-21 21:57:41 +00001073 for (; begin != end; ++begin)
1074 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075
1076 // Just return null for empty protocol lists
1077 if (ProtocolRefs.empty())
1078 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1079
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001080 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001081 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1082
1083 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001084 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1086 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1087 Values[2] =
1088 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1089 ProtocolRefs.size()),
1090 ProtocolRefs);
1091
1092 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1093 llvm::GlobalVariable *GV =
1094 new llvm::GlobalVariable(Init->getType(), false,
1095 llvm::GlobalValue::InternalLinkage,
1096 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001097 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001098 &CGM.getModule());
1099 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1100 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1101}
1102
1103/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001104 struct _objc_property {
1105 const char * const name;
1106 const char * const attributes;
1107 };
1108
1109 struct _objc_property_list {
1110 uint32_t entsize; // sizeof (struct _objc_property)
1111 uint32_t prop_count;
1112 struct _objc_property[prop_count];
1113 };
1114*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001115llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1116 const Decl *Container,
1117 const ObjCContainerDecl *OCD,
1118 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001119 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001120 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1121 E = OCD->prop_end(); I != E; ++I) {
1122 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001123 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001124 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001125 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1126 Prop));
1127 }
1128
1129 // Return null for empty list.
1130 if (Properties.empty())
1131 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1132
1133 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001134 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001135 std::vector<llvm::Constant*> Values(3);
1136 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1137 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1138 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1139 Properties.size());
1140 Values[2] = llvm::ConstantArray::get(AT, Properties);
1141 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1142
1143 llvm::GlobalVariable *GV =
1144 new llvm::GlobalVariable(Init->getType(), false,
1145 llvm::GlobalValue::InternalLinkage,
1146 Init,
1147 Name,
1148 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001149 if (ObjCABI == 2)
1150 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001151 // No special section on property lists?
1152 UsedGlobals.push_back(GV);
1153 return llvm::ConstantExpr::getBitCast(GV,
1154 ObjCTypes.PropertyListPtrTy);
1155
1156}
1157
1158/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001159 struct objc_method_description_list {
1160 int count;
1161 struct objc_method_description list[];
1162 };
1163*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001164llvm::Constant *
1165CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1166 std::vector<llvm::Constant*> Desc(2);
1167 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1168 ObjCTypes.SelectorPtrTy);
1169 Desc[1] = GetMethodVarType(MD);
1170 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1171 Desc);
1172}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001173
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001174llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1175 const char *Section,
1176 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001177 // Return null for empty list.
1178 if (Methods.empty())
1179 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1180
1181 std::vector<llvm::Constant*> Values(2);
1182 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1183 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1184 Methods.size());
1185 Values[1] = llvm::ConstantArray::get(AT, Methods);
1186 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1187
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001188 llvm::GlobalVariable *GV =
1189 new llvm::GlobalVariable(Init->getType(), false,
1190 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001191 Init, Name, &CGM.getModule());
1192 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001193 UsedGlobals.push_back(GV);
1194 return llvm::ConstantExpr::getBitCast(GV,
1195 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001196}
1197
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001198/*
1199 struct _objc_category {
1200 char *category_name;
1201 char *class_name;
1202 struct _objc_method_list *instance_methods;
1203 struct _objc_method_list *class_methods;
1204 struct _objc_protocol_list *protocols;
1205 uint32_t size; // <rdar://4585769>
1206 struct _objc_property_list *instance_properties;
1207 };
1208 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001209void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001210 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001211
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001212 // FIXME: This is poor design, the OCD should have a pointer to the
1213 // category decl. Additionally, note that Category can be null for
1214 // the @implementation w/o an @interface case. Sema should just
1215 // create one for us as it does for @implementation so everyone else
1216 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001217 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001218 const ObjCCategoryDecl *Category =
1219 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001220 std::string ExtName(Interface->getNameAsString() + "_" +
1221 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001222
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001223 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1224 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1225 e = OCD->instmeth_end(); i != e; ++i) {
1226 // Instance methods should always be defined.
1227 InstanceMethods.push_back(GetMethodConstant(*i));
1228 }
1229 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1230 e = OCD->classmeth_end(); i != e; ++i) {
1231 // Class methods should always be defined.
1232 ClassMethods.push_back(GetMethodConstant(*i));
1233 }
1234
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001235 std::vector<llvm::Constant*> Values(7);
1236 Values[0] = GetClassName(OCD->getIdentifier());
1237 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001238 Values[2] =
1239 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1240 ExtName,
1241 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001242 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001243 Values[3] =
1244 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1245 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001246 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001247 if (Category) {
1248 Values[4] =
1249 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1250 Category->protocol_begin(),
1251 Category->protocol_end());
1252 } else {
1253 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1254 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001255 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001256
1257 // If there is no category @interface then there can be no properties.
1258 if (Category) {
1259 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001260 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001261 } else {
1262 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1263 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001264
1265 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1266 Values);
1267
1268 llvm::GlobalVariable *GV =
1269 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1270 llvm::GlobalValue::InternalLinkage,
1271 Init,
1272 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1273 &CGM.getModule());
1274 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1275 UsedGlobals.push_back(GV);
1276 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001277}
1278
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001279// FIXME: Get from somewhere?
1280enum ClassFlags {
1281 eClassFlags_Factory = 0x00001,
1282 eClassFlags_Meta = 0x00002,
1283 // <rdr://5142207>
1284 eClassFlags_HasCXXStructors = 0x02000,
1285 eClassFlags_Hidden = 0x20000,
1286 eClassFlags_ABI2_Hidden = 0x00010,
1287 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1288};
1289
1290// <rdr://5142207&4705298&4843145>
1291static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1292 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1293 // FIXME: Support -fvisibility
1294 switch (attr->getVisibility()) {
1295 default:
1296 assert(0 && "Unknown visibility");
1297 return false;
1298 case VisibilityAttr::DefaultVisibility:
1299 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1300 return false;
1301 case VisibilityAttr::HiddenVisibility:
1302 return true;
1303 }
1304 } else {
1305 return false; // FIXME: Support -fvisibility
1306 }
1307}
1308
1309/*
1310 struct _objc_class {
1311 Class isa;
1312 Class super_class;
1313 const char *name;
1314 long version;
1315 long info;
1316 long instance_size;
1317 struct _objc_ivar_list *ivars;
1318 struct _objc_method_list *methods;
1319 struct _objc_cache *cache;
1320 struct _objc_protocol_list *protocols;
1321 // Objective-C 1.0 extensions (<rdr://4585769>)
1322 const char *ivar_layout;
1323 struct _objc_class_ext *ext;
1324 };
1325
1326 See EmitClassExtension();
1327 */
1328void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001329 DefinedSymbols.insert(ID->getIdentifier());
1330
Chris Lattner8ec03f52008-11-24 03:54:41 +00001331 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001332 // FIXME: Gross
1333 ObjCInterfaceDecl *Interface =
1334 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001335 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001336 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001337 Interface->protocol_begin(),
1338 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001339 const llvm::Type *InterfaceTy =
1340 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1341 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001342 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001343
1344 // FIXME: Set CXX-structors flag.
1345 if (IsClassHidden(ID->getClassInterface()))
1346 Flags |= eClassFlags_Hidden;
1347
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001348 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1349 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1350 e = ID->instmeth_end(); i != e; ++i) {
1351 // Instance methods should always be defined.
1352 InstanceMethods.push_back(GetMethodConstant(*i));
1353 }
1354 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1355 e = ID->classmeth_end(); i != e; ++i) {
1356 // Class methods should always be defined.
1357 ClassMethods.push_back(GetMethodConstant(*i));
1358 }
1359
1360 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1361 e = ID->propimpl_end(); i != e; ++i) {
1362 ObjCPropertyImplDecl *PID = *i;
1363
1364 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1365 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1366
1367 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1368 if (llvm::Constant *C = GetMethodConstant(MD))
1369 InstanceMethods.push_back(C);
1370 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1371 if (llvm::Constant *C = GetMethodConstant(MD))
1372 InstanceMethods.push_back(C);
1373 }
1374 }
1375
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001376 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001377 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001378 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001379 // Record a reference to the super class.
1380 LazySymbols.insert(Super->getIdentifier());
1381
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001382 Values[ 1] =
1383 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1384 ObjCTypes.ClassPtrTy);
1385 } else {
1386 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1387 }
1388 Values[ 2] = GetClassName(ID->getIdentifier());
1389 // Version is always 0.
1390 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1391 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1392 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001393 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001394 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001395 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001396 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001397 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001398 // cache is always NULL.
1399 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1400 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001401 // FIXME: Set ivar_layout
1402 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001403 Values[11] = EmitClassExtension(ID);
1404 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1405 Values);
1406
1407 llvm::GlobalVariable *GV =
1408 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1409 llvm::GlobalValue::InternalLinkage,
1410 Init,
1411 std::string("\01L_OBJC_CLASS_")+ClassName,
1412 &CGM.getModule());
1413 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1414 UsedGlobals.push_back(GV);
1415 // FIXME: Why?
1416 GV->setAlignment(32);
1417 DefinedClasses.push_back(GV);
1418}
1419
1420llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1421 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001422 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001423 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001424 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001425 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001426
1427 if (IsClassHidden(ID->getClassInterface()))
1428 Flags |= eClassFlags_Hidden;
1429
1430 std::vector<llvm::Constant*> Values(12);
1431 // The isa for the metaclass is the root of the hierarchy.
1432 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1433 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1434 Root = Super;
1435 Values[ 0] =
1436 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1437 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001438 // The super class for the metaclass is emitted as the name of the
1439 // super class. The runtime fixes this up to point to the
1440 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001441 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1442 Values[ 1] =
1443 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1444 ObjCTypes.ClassPtrTy);
1445 } else {
1446 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1447 }
1448 Values[ 2] = GetClassName(ID->getIdentifier());
1449 // Version is always 0.
1450 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1451 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1452 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001453 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001454 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001455 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001456 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001457 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001458 // cache is always NULL.
1459 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1460 Values[ 9] = Protocols;
1461 // ivar_layout for metaclass is always NULL.
1462 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1463 // The class extension is always unused for metaclasses.
1464 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1465 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1466 Values);
1467
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001468 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001469 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001470
1471 // Check for a forward reference.
1472 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1473 if (GV) {
1474 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1475 "Forward metaclass reference has incorrect type.");
1476 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1477 GV->setInitializer(Init);
1478 } else {
1479 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1480 llvm::GlobalValue::InternalLinkage,
1481 Init, Name,
1482 &CGM.getModule());
1483 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001484 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1485 UsedGlobals.push_back(GV);
1486 // FIXME: Why?
1487 GV->setAlignment(32);
1488
1489 return GV;
1490}
1491
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001492llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001493 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001494
1495 // FIXME: Should we look these up somewhere other than the
1496 // module. Its a bit silly since we only generate these while
1497 // processing an implementation, so exactly one pointer would work
1498 // if know when we entered/exitted an implementation block.
1499
1500 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001501 // Previously, metaclass with internal linkage may have been defined.
1502 // pass 'true' as 2nd argument so it is returned.
1503 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001504 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1505 "Forward metaclass reference has incorrect type.");
1506 return GV;
1507 } else {
1508 // Generate as an external reference to keep a consistent
1509 // module. This will be patched up when we emit the metaclass.
1510 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1511 llvm::GlobalValue::ExternalLinkage,
1512 0,
1513 Name,
1514 &CGM.getModule());
1515 }
1516}
1517
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001518/*
1519 struct objc_class_ext {
1520 uint32_t size;
1521 const char *weak_ivar_layout;
1522 struct _objc_property_list *properties;
1523 };
1524*/
1525llvm::Constant *
1526CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1527 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001528 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529
1530 std::vector<llvm::Constant*> Values(3);
1531 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001532 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001533 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001534 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001535 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001536
1537 // Return null if no extension bits are used.
1538 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1539 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1540
1541 llvm::Constant *Init =
1542 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1543 llvm::GlobalVariable *GV =
1544 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1545 llvm::GlobalValue::InternalLinkage,
1546 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001547 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001548 &CGM.getModule());
1549 // No special section, but goes in llvm.used
1550 UsedGlobals.push_back(GV);
1551
1552 return GV;
1553}
1554
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001555/// countInheritedIvars - count number of ivars in class and its super class(s)
1556///
1557static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1558 int count = 0;
1559 if (!OI)
1560 return 0;
1561 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1562 if (SuperClass)
1563 count += countInheritedIvars(SuperClass);
1564 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1565 E = OI->ivar_end(); I != E; ++I)
1566 ++count;
1567 return count;
1568}
1569
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570/*
1571 struct objc_ivar {
1572 char *ivar_name;
1573 char *ivar_type;
1574 int ivar_offset;
1575 };
1576
1577 struct objc_ivar_list {
1578 int ivar_count;
1579 struct objc_ivar list[count];
1580 };
1581 */
1582llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001583 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001584 std::vector<llvm::Constant*> Ivars, Ivar(3);
1585
1586 // When emitting the root class GCC emits ivar entries for the
1587 // actual class structure. It is not clear if we need to follow this
1588 // behavior; for now lets try and get away with not doing it. If so,
1589 // the cleanest solution would be to make up an ObjCInterfaceDecl
1590 // for the class.
1591 if (ForClass)
1592 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001593
1594 ObjCInterfaceDecl *OID =
1595 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1596 const llvm::Type *InterfaceTy =
1597 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001598 const llvm::StructLayout *Layout =
1599 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001600
1601 RecordDecl::field_iterator ifield, pfield;
1602 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001603 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1604 FieldDecl *Field = *ifield;
1605 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1606 getLLVMFieldNo(Field));
1607 if (Field->getIdentifier())
1608 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1609 else
1610 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001611 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001612 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001613 Ivar[1] = GetMethodVarType(TypeStr);
1614 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001615 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001616 }
1617
1618 // Return null for empty list.
1619 if (Ivars.empty())
1620 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1621
1622 std::vector<llvm::Constant*> Values(2);
1623 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1624 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1625 Ivars.size());
1626 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1627 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1628
1629 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1630 "\01L_OBJC_INSTANCE_VARIABLES_");
1631 llvm::GlobalVariable *GV =
1632 new llvm::GlobalVariable(Init->getType(), false,
1633 llvm::GlobalValue::InternalLinkage,
1634 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001635 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001636 &CGM.getModule());
1637 if (ForClass) {
1638 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1639 // FIXME: Why is this only here?
1640 GV->setAlignment(32);
1641 } else {
1642 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1643 }
1644 UsedGlobals.push_back(GV);
1645 return llvm::ConstantExpr::getBitCast(GV,
1646 ObjCTypes.IvarListPtrTy);
1647}
1648
1649/*
1650 struct objc_method {
1651 SEL method_name;
1652 char *method_types;
1653 void *method;
1654 };
1655
1656 struct objc_method_list {
1657 struct objc_method_list *obsolete;
1658 int count;
1659 struct objc_method methods_list[count];
1660 };
1661*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001662
1663/// GetMethodConstant - Return a struct objc_method constant for the
1664/// given method if it has been defined. The result is null if the
1665/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001666llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001667 // FIXME: Use DenseMap::lookup
1668 llvm::Function *Fn = MethodDefinitions[MD];
1669 if (!Fn)
1670 return 0;
1671
1672 std::vector<llvm::Constant*> Method(3);
1673 Method[0] =
1674 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1675 ObjCTypes.SelectorPtrTy);
1676 Method[1] = GetMethodVarType(MD);
1677 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1678 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1679}
1680
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001681llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1682 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001683 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001684 // Return null for empty list.
1685 if (Methods.empty())
1686 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1687
1688 std::vector<llvm::Constant*> Values(3);
1689 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1690 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1691 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1692 Methods.size());
1693 Values[2] = llvm::ConstantArray::get(AT, Methods);
1694 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1695
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001696 llvm::GlobalVariable *GV =
1697 new llvm::GlobalVariable(Init->getType(), false,
1698 llvm::GlobalValue::InternalLinkage,
1699 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001700 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001701 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001702 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001703 UsedGlobals.push_back(GV);
1704 return llvm::ConstantExpr::getBitCast(GV,
1705 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001706}
1707
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001708llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001709 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001710 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001711 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001712
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001713 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001714 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001715 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001716 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001717 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001718 llvm::GlobalValue::InternalLinkage,
1719 Name,
1720 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001721 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001722
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001723 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001724}
1725
1726llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001727 // Abuse this interface function as a place to finalize.
1728 FinishModule();
1729
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001730 return NULL;
1731}
1732
Daniel Dunbar49f66022008-09-24 03:38:44 +00001733llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1734 return ObjCTypes.GetPropertyFn;
1735}
1736
1737llvm::Function *CGObjCMac::GetPropertySetFunction() {
1738 return ObjCTypes.SetPropertyFn;
1739}
1740
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001741llvm::Function *CGObjCMac::EnumerationMutationFunction()
1742{
1743 return ObjCTypes.EnumerationMutationFn;
1744}
1745
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001746/*
1747
1748Objective-C setjmp-longjmp (sjlj) Exception Handling
1749--
1750
1751The basic framework for a @try-catch-finally is as follows:
1752{
1753 objc_exception_data d;
1754 id _rethrow = null;
1755
1756 objc_exception_try_enter(&d);
1757 if (!setjmp(d.jmp_buf)) {
1758 ... try body ...
1759 } else {
1760 // exception path
1761 id _caught = objc_exception_extract(&d);
1762
1763 // enter new try scope for handlers
1764 if (!setjmp(d.jmp_buf)) {
1765 ... match exception and execute catch blocks ...
1766
1767 // fell off end, rethrow.
1768 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001769 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001770 } else {
1771 // exception in catch block
1772 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001773 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001774 }
1775 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001776 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001777
1778finally:
1779 // match either the initial try_enter or the catch try_enter,
1780 // depending on the path followed.
1781 objc_exception_try_exit(&d);
1782finally_no_exit:
1783 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001784 ... dispatch to finally destination ...
1785
1786finally_rethrow:
1787 objc_exception_throw(_rethrow);
1788
1789finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001790}
1791
1792This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001793uses _rethrow to determine if objc_exception_try_exit should be called
1794and if the object should be rethrown. This breaks in the face of
1795throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001796
1797We specialize this framework for a few particular circumstances:
1798
1799 - If there are no catch blocks, then we avoid emitting the second
1800 exception handling context.
1801
1802 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1803 e)) we avoid emitting the code to rethrow an uncaught exception.
1804
1805 - FIXME: If there is no @finally block we can do a few more
1806 simplifications.
1807
1808Rethrows and Jumps-Through-Finally
1809--
1810
1811Support for implicit rethrows and jumping through the finally block is
1812handled by storing the current exception-handling context in
1813ObjCEHStack.
1814
Daniel Dunbar898d5082008-09-30 01:06:03 +00001815In order to implement proper @finally semantics, we support one basic
1816mechanism for jumping through the finally block to an arbitrary
1817destination. Constructs which generate exits from a @try or @catch
1818block use this mechanism to implement the proper semantics by chaining
1819jumps, as necessary.
1820
1821This mechanism works like the one used for indirect goto: we
1822arbitrarily assign an ID to each destination and store the ID for the
1823destination in a variable prior to entering the finally block. At the
1824end of the finally block we simply create a switch to the proper
1825destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001826
1827Code gen for @synchronized(expr) stmt;
1828Effectively generating code for:
1829objc_sync_enter(expr);
1830@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001831*/
1832
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001833void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1834 const Stmt &S) {
1835 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001836 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001837 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1838 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1839 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1840 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001841 llvm::Value *DestCode =
1842 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1843
1844 // Generate jump code. Done here so we can directly add things to
1845 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001846 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001847 llvm::SwitchInst *FinallySwitch =
1848 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1849 FinallyEnd, 10, FinallyJump);
1850
1851 // Push an EH context entry, used for handling rethrows and jumps
1852 // through finally.
1853 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1854 FinallySwitch, DestCode);
1855 CGF.ObjCEHStack.push_back(&EHEntry);
1856
1857 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001858 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1859 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001860 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1861 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001862 if (!isTry) {
1863 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001864 llvm::Value *Arg = CGF.EmitScalarExpr(
1865 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1866 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1867 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001868 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001869
1870 // Enter a new try block and call setjmp.
1871 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1872 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1873 "jmpbufarray");
1874 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1875 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1876 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001877
Daniel Dunbar55e87422008-11-11 02:29:29 +00001878 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1879 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001880 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001881 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001882
1883 // Emit the @try block.
1884 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001885 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1886 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001887 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001888
1889 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001890 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001891
1892 // Retrieve the exception object. We may emit multiple blocks but
1893 // nothing can cross this so the value is already in SSA form.
1894 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1895 ExceptionData,
1896 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001897 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001898 if (!isTry)
1899 {
1900 CGF.Builder.CreateStore(Caught, RethrowPtr);
1901 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1902 }
1903 else if (const ObjCAtCatchStmt* CatchStmt =
1904 cast<ObjCAtTryStmt>(S).getCatchStmts())
1905 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001906 // Enter a new exception try block (in case a @catch block throws
1907 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001908 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001909
Anders Carlsson80f25672008-09-09 17:59:25 +00001910 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1911 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001912 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001913
Daniel Dunbar55e87422008-11-11 02:29:29 +00001914 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1915 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001916 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001917
1918 CGF.EmitBlock(CatchBlock);
1919
Daniel Dunbar55e40722008-09-27 07:03:52 +00001920 // Handle catch list. As a special case we check if everything is
1921 // matched and avoid generating code for falling off the end if
1922 // so.
1923 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001924 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001925 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001926
Anders Carlssondde0a942008-09-11 09:15:33 +00001927 const DeclStmt *CatchParam =
1928 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001929 const VarDecl *VD = 0;
1930 const PointerType *PT = 0;
1931
Anders Carlsson80f25672008-09-09 17:59:25 +00001932 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001933 if (!CatchParam) {
1934 AllMatched = true;
1935 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001936 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001937 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001938
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001939 // catch(id e) always matches.
1940 // FIXME: For the time being we also match id<X>; this should
1941 // be rejected by Sema instead.
1942 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1943 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001944 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001945 }
1946
Daniel Dunbar55e40722008-09-27 07:03:52 +00001947 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001948 if (CatchParam) {
1949 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001950 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001951 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001952 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001953
Anders Carlssondde0a942008-09-11 09:15:33 +00001954 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001955 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001956 break;
1957 }
1958
Daniel Dunbar129271a2008-09-27 07:36:24 +00001959 assert(PT && "Unexpected non-pointer type in @catch");
1960 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001961 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001962 assert(ObjCType && "Catch parameter must have Objective-C type!");
1963
1964 // Check if the @catch block matches the exception object.
1965 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1966
Anders Carlsson80f25672008-09-09 17:59:25 +00001967 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1968 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001969
Daniel Dunbar55e87422008-11-11 02:29:29 +00001970 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001971
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001972 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001973 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001974
1975 // Emit the @catch block.
1976 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001977 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001978 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001979
1980 llvm::Value *Tmp =
1981 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1982 "tmp");
1983 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001984
1985 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001986 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001987
1988 CGF.EmitBlock(NextCatchBlock);
1989 }
1990
Daniel Dunbar55e40722008-09-27 07:03:52 +00001991 if (!AllMatched) {
1992 // None of the handlers caught the exception, so store it to be
1993 // rethrown at the end of the @finally block.
1994 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001995 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001996 }
1997
1998 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001999 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002000 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2001 ExceptionData),
2002 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002003 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002004 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002005 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002006 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00002007 }
2008
Daniel Dunbar898d5082008-09-30 01:06:03 +00002009 // Pop the exception-handling stack entry. It is important to do
2010 // this now, because the code in the @finally block is not in this
2011 // context.
2012 CGF.ObjCEHStack.pop_back();
2013
Anders Carlsson80f25672008-09-09 17:59:25 +00002014 // Emit the @finally block.
2015 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002016 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002017
2018 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002019 if (isTry) {
2020 if (const ObjCAtFinallyStmt* FinallyStmt =
2021 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2022 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2023 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002024 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002025 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002026 // For @synchronized, call objc_sync_enter(sync.expr)
2027 llvm::Value *Arg = CGF.EmitScalarExpr(
2028 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2029 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2030 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2031 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002032
Daniel Dunbar898d5082008-09-30 01:06:03 +00002033 CGF.EmitBlock(FinallyJump);
2034
2035 CGF.EmitBlock(FinallyRethrow);
2036 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2037 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002038 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002039
2040 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002041}
2042
2043void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002044 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002045 llvm::Value *ExceptionAsObject;
2046
2047 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2048 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2049 ExceptionAsObject =
2050 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2051 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002052 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002053 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002054 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002055 }
2056
2057 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002058 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002059
2060 // Clear the insertion point to indicate we are in unreachable code.
2061 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002062}
2063
Daniel Dunbar898d5082008-09-30 01:06:03 +00002064void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2065 llvm::BasicBlock *Dst,
2066 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002067 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002068 return;
2069
2070 // Find the destination code for this block. We always use 0 for the
2071 // fallthrough block (default destination).
2072 llvm::SwitchInst *SI = E->FinallySwitch;
2073 llvm::ConstantInt *ID;
2074 if (Dst == SI->getDefaultDest()) {
2075 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2076 } else {
2077 ID = SI->findCaseDest(Dst);
2078 if (!ID) {
2079 // No code found, get a new unique one by just using the number
2080 // of switch successors.
2081 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2082 SI->addCase(ID, Dst);
2083 }
2084 }
2085
2086 // Set the destination code and branch.
2087 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002088 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002089}
2090
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002091/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002092/// object: objc_read_weak (id *src)
2093///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002094llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002095 llvm::Value *AddrWeakObj)
2096{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002097 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002098 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002099 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002100 return read_weak;
2101}
2102
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002103/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2104/// objc_assign_weak (id src, id *dst)
2105///
2106void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2107 llvm::Value *src, llvm::Value *dst)
2108{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002109 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2110 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002111 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2112 src, dst, "weakassign");
2113 return;
2114}
2115
Fariborz Jahanian58626502008-11-19 00:59:10 +00002116/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2117/// objc_assign_global (id src, id *dst)
2118///
2119void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2120 llvm::Value *src, llvm::Value *dst)
2121{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002122 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2123 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002124 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2125 src, dst, "globalassign");
2126 return;
2127}
2128
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002129/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2130/// objc_assign_ivar (id src, id *dst)
2131///
2132void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2133 llvm::Value *src, llvm::Value *dst)
2134{
2135 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2136 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2137 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2138 src, dst, "assignivar");
2139 return;
2140}
2141
Fariborz Jahanian58626502008-11-19 00:59:10 +00002142/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2143/// objc_assign_strongCast (id src, id *dst)
2144///
2145void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2146 llvm::Value *src, llvm::Value *dst)
2147{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002148 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2149 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002150 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2151 src, dst, "weakassign");
2152 return;
2153}
2154
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002155/// EmitObjCValueForIvar - Code Gen for ivar reference.
2156///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002157LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2158 QualType ObjectTy,
2159 llvm::Value *BaseValue,
2160 const ObjCIvarDecl *Ivar,
2161 const FieldDecl *Field,
2162 unsigned CVRQualifiers) {
2163 if (Ivar->isBitField())
2164 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2165 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002166 // TODO: Add a special case for isa (index 0)
2167 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2168 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002169 LValue LV = LValue::MakeAddr(V,
2170 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2171 LValue::SetObjCIvar(LV, true);
2172 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002173}
2174
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002175/* *** Private Interface *** */
2176
2177/// EmitImageInfo - Emit the image info marker used to encode some module
2178/// level information.
2179///
2180/// See: <rdr://4810609&4810587&4810587>
2181/// struct IMAGE_INFO {
2182/// unsigned version;
2183/// unsigned flags;
2184/// };
2185enum ImageInfoFlags {
2186 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2187 eImageInfo_GarbageCollected = (1 << 1),
2188 eImageInfo_GCOnly = (1 << 2)
2189};
2190
2191void CGObjCMac::EmitImageInfo() {
2192 unsigned version = 0; // Version is unused?
2193 unsigned flags = 0;
2194
2195 // FIXME: Fix and continue?
2196 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2197 flags |= eImageInfo_GarbageCollected;
2198 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2199 flags |= eImageInfo_GCOnly;
2200
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002201 // Emitted as int[2];
2202 llvm::Constant *values[2] = {
2203 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2204 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2205 };
2206 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002207 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002208 new llvm::GlobalVariable(AT, true,
2209 llvm::GlobalValue::InternalLinkage,
2210 llvm::ConstantArray::get(AT, values, 2),
2211 "\01L_OBJC_IMAGE_INFO",
2212 &CGM.getModule());
2213
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002214 if (ObjCABI == 1) {
2215 GV->setSection("__OBJC, __image_info,regular");
2216 } else {
2217 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2218 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002219
2220 UsedGlobals.push_back(GV);
2221}
2222
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002223
2224// struct objc_module {
2225// unsigned long version;
2226// unsigned long size;
2227// const char *name;
2228// Symtab symtab;
2229// };
2230
2231// FIXME: Get from somewhere
2232static const int ModuleVersion = 7;
2233
2234void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002235 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002236
2237 std::vector<llvm::Constant*> Values(4);
2238 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2239 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002240 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002241 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002242 Values[3] = EmitModuleSymbols();
2243
2244 llvm::GlobalVariable *GV =
2245 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2246 llvm::GlobalValue::InternalLinkage,
2247 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2248 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002249 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002250 &CGM.getModule());
2251 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2252 UsedGlobals.push_back(GV);
2253}
2254
2255llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 unsigned NumClasses = DefinedClasses.size();
2257 unsigned NumCategories = DefinedCategories.size();
2258
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002259 // Return null if no symbols were defined.
2260 if (!NumClasses && !NumCategories)
2261 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2262
2263 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002264 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2265 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2266 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2267 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2268
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002269 // The runtime expects exactly the list of defined classes followed
2270 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002271 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002272 for (unsigned i=0; i<NumClasses; i++)
2273 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2274 ObjCTypes.Int8PtrTy);
2275 for (unsigned i=0; i<NumCategories; i++)
2276 Symbols[NumClasses + i] =
2277 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2278 ObjCTypes.Int8PtrTy);
2279
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002280 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002281 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 NumClasses + NumCategories),
2283 Symbols);
2284
2285 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2286
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002287 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002288 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002289 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002291 "\01L_OBJC_SYMBOLS",
2292 &CGM.getModule());
2293 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2294 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002295 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2296}
2297
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002298llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002299 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002300 LazySymbols.insert(ID->getIdentifier());
2301
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002302 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2303
2304 if (!Entry) {
2305 llvm::Constant *Casted =
2306 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2307 ObjCTypes.ClassPtrTy);
2308 Entry =
2309 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2310 llvm::GlobalValue::InternalLinkage,
2311 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2312 &CGM.getModule());
2313 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2314 UsedGlobals.push_back(Entry);
2315 }
2316
2317 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002318}
2319
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002320llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002321 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2322
2323 if (!Entry) {
2324 llvm::Constant *Casted =
2325 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2326 ObjCTypes.SelectorPtrTy);
2327 Entry =
2328 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2329 llvm::GlobalValue::InternalLinkage,
2330 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2331 &CGM.getModule());
2332 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2333 UsedGlobals.push_back(Entry);
2334 }
2335
2336 return Builder.CreateLoad(Entry, false, "tmp");
2337}
2338
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002339llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002340 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002341
2342 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002343 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002344 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002345 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002346 llvm::GlobalValue::InternalLinkage,
2347 C, "\01L_OBJC_CLASS_NAME_",
2348 &CGM.getModule());
2349 Entry->setSection("__TEXT,__cstring,cstring_literals");
2350 UsedGlobals.push_back(Entry);
2351 }
2352
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002353 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002354}
2355
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002356llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002357 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2358
2359 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002360 // FIXME: Avoid std::string copying.
2361 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002362 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002363 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002364 llvm::GlobalValue::InternalLinkage,
2365 C, "\01L_OBJC_METH_VAR_NAME_",
2366 &CGM.getModule());
2367 Entry->setSection("__TEXT,__cstring,cstring_literals");
2368 UsedGlobals.push_back(Entry);
2369 }
2370
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002371 return getConstantGEP(Entry, 0, 0);
2372}
2373
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002374// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002375llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002376 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2377}
2378
2379// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002380llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2382}
2383
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002384llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002385 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002386
2387 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002388 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002389 Entry =
2390 new llvm::GlobalVariable(C->getType(), false,
2391 llvm::GlobalValue::InternalLinkage,
2392 C, "\01L_OBJC_METH_VAR_TYPE_",
2393 &CGM.getModule());
2394 Entry->setSection("__TEXT,__cstring,cstring_literals");
2395 UsedGlobals.push_back(Entry);
2396 }
2397
2398 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002399}
2400
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002401// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002402llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002403 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002404 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2405 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002406 return GetMethodVarType(TypeStr);
2407}
2408
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002409// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002410llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002411 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2412
2413 if (!Entry) {
2414 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2415 Entry =
2416 new llvm::GlobalVariable(C->getType(), false,
2417 llvm::GlobalValue::InternalLinkage,
2418 C, "\01L_OBJC_PROP_NAME_ATTR_",
2419 &CGM.getModule());
2420 Entry->setSection("__TEXT,__cstring,cstring_literals");
2421 UsedGlobals.push_back(Entry);
2422 }
2423
2424 return getConstantGEP(Entry, 0, 0);
2425}
2426
2427// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002428// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002429llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002430 const Decl *Container) {
2431 std::string TypeStr;
2432 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002433 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2434}
2435
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002436void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2437 const ObjCContainerDecl *CD,
2438 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002439 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002440 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002441 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002442 assert (CD && "Missing container decl in GetNameForMethod");
2443 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002444 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2445 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002446 NameOut += ' ';
2447 NameOut += D->getSelector().getAsString();
2448 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002449}
2450
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002451/// GetFirstIvarInRecord - This routine returns the record for the
2452/// implementation of the fiven class OID. It also returns field
2453/// corresponding to the first ivar in the class in FIV. It also
2454/// returns the one before the first ivar.
2455///
2456const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2457 const ObjCInterfaceDecl *OID,
2458 RecordDecl::field_iterator &FIV,
2459 RecordDecl::field_iterator &PIV) {
2460 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2461 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2462 RecordDecl::field_iterator ifield = RD->field_begin();
2463 RecordDecl::field_iterator pfield = RD->field_end();
2464 while (countSuperClassIvars-- > 0) {
2465 pfield = ifield;
2466 ++ifield;
2467 }
2468 FIV = ifield;
2469 PIV = pfield;
2470 return RD;
2471}
2472
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002473void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002474 EmitModuleInfo();
2475
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002476 // Emit the dummy bodies for any protocols which were referenced but
2477 // never defined.
2478 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2479 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2480 if (i->second->hasInitializer())
2481 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002482
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002483 std::vector<llvm::Constant*> Values(5);
2484 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2485 Values[1] = GetClassName(i->first);
2486 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2487 Values[3] = Values[4] =
2488 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2489 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2490 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2491 Values));
2492 }
2493
2494 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002495 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002496 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002497 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002498 }
2499
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002501 llvm::GlobalValue *GV =
2502 new llvm::GlobalVariable(AT, false,
2503 llvm::GlobalValue::AppendingLinkage,
2504 llvm::ConstantArray::get(AT, Used),
2505 "llvm.used",
2506 &CGM.getModule());
2507
2508 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002509
2510 // Add assembler directives to add lazy undefined symbol references
2511 // for classes which are referenced but not defined. This is
2512 // important for correct linker interaction.
2513
2514 // FIXME: Uh, this isn't particularly portable.
2515 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002516
2517 if (!CGM.getModule().getModuleInlineAsm().empty())
2518 s << "\n";
2519
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002520 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2521 e = LazySymbols.end(); i != e; ++i) {
2522 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2523 }
2524 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2525 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002526 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002527 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2528 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002529
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002530 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002531}
2532
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002533CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002534 : CGObjCCommonMac(cgm),
2535 ObjCTypes(cgm)
2536{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002537 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002538 ObjCABI = 2;
2539}
2540
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002541/* *** */
2542
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002543ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2544: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002545{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002546 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2547 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002548
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002549 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002550 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002551 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002552 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2553
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002554 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002555 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002556 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002557
2558 // FIXME: It would be nice to unify this with the opaque type, so
2559 // that the IR comes out a bit cleaner.
2560 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2561 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002562
2563 // I'm not sure I like this. The implicit coordination is a bit
2564 // gross. We should solve this in a reasonable fashion because this
2565 // is a pretty common task (match some runtime data structure with
2566 // an LLVM data structure).
2567
2568 // FIXME: This is leaked.
2569 // FIXME: Merge with rewriter code?
2570
2571 // struct _objc_super {
2572 // id self;
2573 // Class cls;
2574 // }
2575 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2576 SourceLocation(),
2577 &Ctx.Idents.get("_objc_super"));
2578 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2579 Ctx.getObjCIdType(), 0, false));
2580 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2581 Ctx.getObjCClassType(), 0, false));
2582 RD->completeDefinition(Ctx);
2583
2584 SuperCTy = Ctx.getTagDeclType(RD);
2585 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2586
2587 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002588 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2589
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002590 // struct _prop_t {
2591 // char *name;
2592 // char *attributes;
2593 // }
2594 PropertyTy = llvm::StructType::get(Int8PtrTy,
2595 Int8PtrTy,
2596 NULL);
2597 CGM.getModule().addTypeName("struct._prop_t",
2598 PropertyTy);
2599
2600 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002601 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002602 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002603 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002604 // }
2605 PropertyListTy = llvm::StructType::get(IntTy,
2606 IntTy,
2607 llvm::ArrayType::get(PropertyTy, 0),
2608 NULL);
2609 CGM.getModule().addTypeName("struct._prop_list_t",
2610 PropertyListTy);
2611 // struct _prop_list_t *
2612 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2613
2614 // struct _objc_method {
2615 // SEL _cmd;
2616 // char *method_type;
2617 // char *_imp;
2618 // }
2619 MethodTy = llvm::StructType::get(SelectorPtrTy,
2620 Int8PtrTy,
2621 Int8PtrTy,
2622 NULL);
2623 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002624
2625 // struct _objc_cache *
2626 CacheTy = llvm::OpaqueType::get();
2627 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2628 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002629
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002630 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002631
2632 QualType IdType = Ctx.getObjCIdType();
2633 QualType SelType = Ctx.getObjCSelType();
2634 llvm::SmallVector<QualType,16> Params;
2635 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002636
2637 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002638 Params.push_back(IdType);
2639 Params.push_back(SelType);
2640 Params.push_back(Ctx.LongTy);
2641 Params.push_back(Ctx.BoolTy);
2642 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2643 false);
2644 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002645
2646 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2647 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002648 Params.push_back(IdType);
2649 Params.push_back(SelType);
2650 Params.push_back(Ctx.LongTy);
2651 Params.push_back(IdType);
2652 Params.push_back(Ctx.BoolTy);
2653 Params.push_back(Ctx.BoolTy);
2654 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2655 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2656
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002657 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002658
2659 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002660 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002661 Params.push_back(IdType);
2662 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2663 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2664 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002665
2666 // gc's API
2667 // id objc_read_weak (id *)
2668 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002669 Params.push_back(Ctx.getPointerType(IdType));
2670 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2671 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2672
2673 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002674 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002675 Params.push_back(IdType);
2676 Params.push_back(Ctx.getPointerType(IdType));
2677
2678 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2679 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2680 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2681 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2682 GcAssignStrongCastFn =
2683 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002684}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002685
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002686ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2687 : ObjCCommonTypesHelper(cgm)
2688{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002689 // struct _objc_method_description {
2690 // SEL name;
2691 // char *types;
2692 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002693 MethodDescriptionTy =
2694 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002695 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002696 NULL);
2697 CGM.getModule().addTypeName("struct._objc_method_description",
2698 MethodDescriptionTy);
2699
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002700 // struct _objc_method_description_list {
2701 // int count;
2702 // struct _objc_method_description[1];
2703 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002704 MethodDescriptionListTy =
2705 llvm::StructType::get(IntTy,
2706 llvm::ArrayType::get(MethodDescriptionTy, 0),
2707 NULL);
2708 CGM.getModule().addTypeName("struct._objc_method_description_list",
2709 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002710
2711 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712 MethodDescriptionListPtrTy =
2713 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2714
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002715 // Protocol description structures
2716
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002717 // struct _objc_protocol_extension {
2718 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2719 // struct _objc_method_description_list *optional_instance_methods;
2720 // struct _objc_method_description_list *optional_class_methods;
2721 // struct _objc_property_list *instance_properties;
2722 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002723 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002724 llvm::StructType::get(IntTy,
2725 MethodDescriptionListPtrTy,
2726 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002727 PropertyListPtrTy,
2728 NULL);
2729 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2730 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002731
2732 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002733 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2734
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002735 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002736
2737 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2738 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2739
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002740 const llvm::Type *T =
2741 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2742 LongTy,
2743 llvm::ArrayType::get(ProtocolTyHolder, 0),
2744 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002745 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2746
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002747 // struct _objc_protocol {
2748 // struct _objc_protocol_extension *isa;
2749 // char *protocol_name;
2750 // struct _objc_protocol **_objc_protocol_list;
2751 // struct _objc_method_description_list *instance_methods;
2752 // struct _objc_method_description_list *class_methods;
2753 // }
2754 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002755 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002756 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2757 MethodDescriptionListPtrTy,
2758 MethodDescriptionListPtrTy,
2759 NULL);
2760 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2761
2762 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2763 CGM.getModule().addTypeName("struct._objc_protocol_list",
2764 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002765 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002766 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2767
2768 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002769 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002770 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002771
2772 // Class description structures
2773
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002774 // struct _objc_ivar {
2775 // char *ivar_name;
2776 // char *ivar_type;
2777 // int ivar_offset;
2778 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002779 IvarTy = llvm::StructType::get(Int8PtrTy,
2780 Int8PtrTy,
2781 IntTy,
2782 NULL);
2783 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2784
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002785 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002786 IvarListTy = llvm::OpaqueType::get();
2787 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2788 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2789
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002790 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002791 MethodListTy = llvm::OpaqueType::get();
2792 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2793 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2794
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002795 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002796 ClassExtensionTy =
2797 llvm::StructType::get(IntTy,
2798 Int8PtrTy,
2799 PropertyListPtrTy,
2800 NULL);
2801 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2802 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2803
2804 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2805
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002806 // struct _objc_class {
2807 // Class isa;
2808 // Class super_class;
2809 // char *name;
2810 // long version;
2811 // long info;
2812 // long instance_size;
2813 // struct _objc_ivar_list *ivars;
2814 // struct _objc_method_list *methods;
2815 // struct _objc_cache *cache;
2816 // struct _objc_protocol_list *protocols;
2817 // char *ivar_layout;
2818 // struct _objc_class_ext *ext;
2819 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002820 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2821 llvm::PointerType::getUnqual(ClassTyHolder),
2822 Int8PtrTy,
2823 LongTy,
2824 LongTy,
2825 LongTy,
2826 IvarListPtrTy,
2827 MethodListPtrTy,
2828 CachePtrTy,
2829 ProtocolListPtrTy,
2830 Int8PtrTy,
2831 ClassExtensionPtrTy,
2832 NULL);
2833 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2834
2835 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2836 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2837 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2838
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002839 // struct _objc_category {
2840 // char *category_name;
2841 // char *class_name;
2842 // struct _objc_method_list *instance_method;
2843 // struct _objc_method_list *class_method;
2844 // uint32_t size; // sizeof(struct _objc_category)
2845 // struct _objc_property_list *instance_properties;// category's @property
2846 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002847 CategoryTy = llvm::StructType::get(Int8PtrTy,
2848 Int8PtrTy,
2849 MethodListPtrTy,
2850 MethodListPtrTy,
2851 ProtocolListPtrTy,
2852 IntTy,
2853 PropertyListPtrTy,
2854 NULL);
2855 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2856
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002857 // Global metadata structures
2858
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002859 // struct _objc_symtab {
2860 // long sel_ref_cnt;
2861 // SEL *refs;
2862 // short cls_def_cnt;
2863 // short cat_def_cnt;
2864 // char *defs[cls_def_cnt + cat_def_cnt];
2865 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002866 SymtabTy = llvm::StructType::get(LongTy,
2867 SelectorPtrTy,
2868 ShortTy,
2869 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002870 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002871 NULL);
2872 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2873 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2874
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002875 // struct _objc_module {
2876 // long version;
2877 // long size; // sizeof(struct _objc_module)
2878 // char *name;
2879 // struct _objc_symtab* symtab;
2880 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002881 ModuleTy =
2882 llvm::StructType::get(LongTy,
2883 LongTy,
2884 Int8PtrTy,
2885 SymtabPtrTy,
2886 NULL);
2887 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002888
Daniel Dunbar49f66022008-09-24 03:38:44 +00002889 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002890
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002891 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002892 std::vector<const llvm::Type*> Params;
2893 Params.push_back(ObjectPtrTy);
2894 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002895 MessageSendFn =
2896 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2897 Params,
2898 true),
2899 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002900
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002901 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002902 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002903 Params.push_back(ObjectPtrTy);
2904 Params.push_back(SelectorPtrTy);
2905 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002906 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2907 Params,
2908 true),
2909 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002910
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002911 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002912 Params.clear();
2913 Params.push_back(ObjectPtrTy);
2914 Params.push_back(SelectorPtrTy);
2915 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002916 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002917 MessageSendFpretFn =
2918 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2919 Params,
2920 true),
2921 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002922
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002923 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002924 Params.clear();
2925 Params.push_back(SuperPtrTy);
2926 Params.push_back(SelectorPtrTy);
2927 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002928 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2929 Params,
2930 true),
2931 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002932
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002933 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2934 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002935 Params.clear();
2936 Params.push_back(Int8PtrTy);
2937 Params.push_back(SuperPtrTy);
2938 Params.push_back(SelectorPtrTy);
2939 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002940 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2941 Params,
2942 true),
2943 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002944
2945 // There is no objc_msgSendSuper_fpret? How can that work?
2946 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002947
Anders Carlsson124526b2008-09-09 10:10:21 +00002948 // FIXME: This is the size of the setjmp buffer and should be
2949 // target specific. 18 is what's used on 32-bit X86.
2950 uint64_t SetJmpBufferSize = 18;
2951
2952 // Exceptions
2953 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002954 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002955
2956 ExceptionDataTy =
2957 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2958 SetJmpBufferSize),
2959 StackPtrTy, NULL);
2960 CGM.getModule().addTypeName("struct._objc_exception_data",
2961 ExceptionDataTy);
2962
2963 Params.clear();
2964 Params.push_back(ObjectPtrTy);
2965 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002966 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2967 Params,
2968 false),
2969 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002970
2971 Params.clear();
2972 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2973 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002974 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2975 Params,
2976 false),
2977 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002978 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002979 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2980 Params,
2981 false),
2982 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002983 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002984 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2985 Params,
2986 false),
2987 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002988
2989 Params.clear();
2990 Params.push_back(ClassPtrTy);
2991 Params.push_back(ObjectPtrTy);
2992 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002993 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2994 Params,
2995 false),
2996 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002997
2998 // synchronized APIs
2999 // void objc_sync_enter (id)
3000 Params.clear();
3001 Params.push_back(ObjectPtrTy);
3002 SyncEnterFn =
3003 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3004 Params,
3005 false),
3006 "objc_sync_enter");
3007 // void objc_sync_exit (id)
3008 SyncExitFn =
3009 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3010 Params,
3011 false),
3012 "objc_sync_exit");
3013
Anders Carlsson124526b2008-09-09 10:10:21 +00003014
3015 Params.clear();
3016 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3017 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003018 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3019 Params,
3020 false),
3021 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003022
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003023}
3024
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003025ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003026: ObjCCommonTypesHelper(cgm)
3027{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003028 // struct _method_list_t {
3029 // uint32_t entsize; // sizeof(struct _objc_method)
3030 // uint32_t method_count;
3031 // struct _objc_method method_list[method_count];
3032 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003033 MethodListnfABITy = llvm::StructType::get(IntTy,
3034 IntTy,
3035 llvm::ArrayType::get(MethodTy, 0),
3036 NULL);
3037 CGM.getModule().addTypeName("struct.__method_list_t",
3038 MethodListnfABITy);
3039 // struct method_list_t *
3040 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003041
3042 // struct _protocol_t {
3043 // id isa; // NULL
3044 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003045 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003046 // const struct method_list_t * const instance_methods;
3047 // const struct method_list_t * const class_methods;
3048 // const struct method_list_t *optionalInstanceMethods;
3049 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003050 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003051 // const uint32_t size; // sizeof(struct _protocol_t)
3052 // const uint32_t flags; // = 0
3053 // }
3054
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003055 // Holder for struct _protocol_list_t *
3056 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3057
3058 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3059 Int8PtrTy,
3060 llvm::PointerType::getUnqual(
3061 ProtocolListTyHolder),
3062 MethodListnfABIPtrTy,
3063 MethodListnfABIPtrTy,
3064 MethodListnfABIPtrTy,
3065 MethodListnfABIPtrTy,
3066 PropertyListPtrTy,
3067 IntTy,
3068 IntTy,
3069 NULL);
3070 CGM.getModule().addTypeName("struct._protocol_t",
3071 ProtocolnfABITy);
3072
Fariborz Jahanianda320092009-01-29 19:24:30 +00003073 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003074 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003075 // struct _protocol_t[protocol_count];
3076 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003077 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3078 llvm::ArrayType::get(
3079 ProtocolnfABITy, 0),
3080 NULL);
3081 CGM.getModule().addTypeName("struct._objc_protocol_list",
3082 ProtocolListnfABITy);
3083
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003084 // struct _objc_protocol_list*
3085 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003086
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003087 // FIXME! Is this doing the right thing?
3088 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3089 ProtocolListnfABIPtrTy);
3090
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003091 // struct _ivar_t {
3092 // unsigned long int *offset; // pointer to ivar offset location
3093 // char *name;
3094 // char *type;
3095 // uint32_t alignment;
3096 // uint32_t size;
3097 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003098 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3099 Int8PtrTy,
3100 Int8PtrTy,
3101 IntTy,
3102 IntTy,
3103 NULL);
3104 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3105
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003106 // struct _ivar_list_t {
3107 // uint32 entsize; // sizeof(struct _ivar_t)
3108 // uint32 count;
3109 // struct _iver_t list[count];
3110 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003111 IvarListnfABITy = llvm::StructType::get(IntTy,
3112 IntTy,
3113 llvm::ArrayType::get(
3114 IvarnfABITy, 0),
3115 NULL);
3116 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3117
3118 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003119
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003120 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003121 // uint32_t const flags;
3122 // uint32_t const instanceStart;
3123 // uint32_t const instanceSize;
3124 // uint32_t const reserved; // only when building for 64bit targets
3125 // const uint8_t * const ivarLayout;
3126 // const char *const name;
3127 // const struct _method_list_t * const baseMethods;
3128 // const struct _objc_protocol_list *const baseProtocols;
3129 // const struct _ivar_list_t *const ivars;
3130 // const uint8_t * const weakIvarLayout;
3131 // const struct _prop_list_t * const properties;
3132 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003133
3134 // FIXME. Add 'reserved' field in 64bit abi mode!
3135 ClassRonfABITy = llvm::StructType::get(IntTy,
3136 IntTy,
3137 IntTy,
3138 Int8PtrTy,
3139 Int8PtrTy,
3140 MethodListnfABIPtrTy,
3141 ProtocolListnfABIPtrTy,
3142 IvarListnfABIPtrTy,
3143 Int8PtrTy,
3144 PropertyListPtrTy,
3145 NULL);
3146 CGM.getModule().addTypeName("struct._class_ro_t",
3147 ClassRonfABITy);
3148
3149 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3150 std::vector<const llvm::Type*> Params;
3151 Params.push_back(ObjectPtrTy);
3152 Params.push_back(SelectorPtrTy);
3153 ImpnfABITy = llvm::PointerType::getUnqual(
3154 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3155
3156 // struct _class_t {
3157 // struct _class_t *isa;
3158 // struct _class_t * const superclass;
3159 // void *cache;
3160 // IMP *vtable;
3161 // struct class_ro_t *ro;
3162 // }
3163
3164 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3165 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3166 llvm::PointerType::getUnqual(ClassTyHolder),
3167 CachePtrTy,
3168 llvm::PointerType::getUnqual(ImpnfABITy),
3169 llvm::PointerType::getUnqual(
3170 ClassRonfABITy),
3171 NULL);
3172 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3173
3174 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3175 ClassnfABITy);
3176
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003177 // LLVM for struct _class_t *
3178 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3179
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003180 // struct _category_t {
3181 // const char * const name;
3182 // struct _class_t *const cls;
3183 // const struct _method_list_t * const instance_methods;
3184 // const struct _method_list_t * const class_methods;
3185 // const struct _protocol_list_t * const protocols;
3186 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003187 // }
3188 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003189 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003190 MethodListnfABIPtrTy,
3191 MethodListnfABIPtrTy,
3192 ProtocolListnfABIPtrTy,
3193 PropertyListPtrTy,
3194 NULL);
3195 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003196
3197 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003198 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3199 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003200
3201 // MessageRefTy - LLVM for:
3202 // struct _message_ref_t {
3203 // IMP messenger;
3204 // SEL name;
3205 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003206
3207 // First the clang type for struct _message_ref_t
3208 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3209 SourceLocation(),
3210 &Ctx.Idents.get("_message_ref_t"));
3211 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3212 Ctx.VoidPtrTy, 0, false));
3213 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3214 Ctx.getObjCSelType(), 0, false));
3215 RD->completeDefinition(Ctx);
3216
3217 MessageRefCTy = Ctx.getTagDeclType(RD);
3218 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3219 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003220
3221 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3222 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3223
3224 // SuperMessageRefTy - LLVM for:
3225 // struct _super_message_ref_t {
3226 // SUPER_IMP messenger;
3227 // SEL name;
3228 // };
3229 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3230 SelectorPtrTy,
3231 NULL);
3232 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3233
3234 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3235 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3236
3237 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3238 Params.clear();
3239 Params.push_back(ObjectPtrTy);
3240 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003241 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3242 Params,
3243 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003244 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003245 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003246 "objc_msgSend_fixup");
3247
3248 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3249 MessageSendFpretFixupFn =
3250 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3251 Params,
3252 true),
3253 "objc_msgSend_fpret_fixup");
3254
3255 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3256 MessageSendStretFixupFn =
3257 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3258 Params,
3259 true),
3260 "objc_msgSend_stret_fixup");
3261
3262 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3263 MessageSendIdFixupFn =
3264 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3265 Params,
3266 true),
3267 "objc_msgSendId_fixup");
3268
3269
3270 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3271 MessageSendIdStretFixupFn =
3272 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3273 Params,
3274 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003275 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003276
3277 // id objc_msgSendSuper2_fixup (struct objc_super *,
3278 // struct _super_message_ref_t*, ...)
3279 Params.clear();
3280 Params.push_back(SuperPtrTy);
3281 Params.push_back(SuperMessageRefPtrTy);
3282 MessageSendSuper2FixupFn =
3283 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3284 Params,
3285 true),
3286 "objc_msgSendSuper2_fixup");
3287
3288
3289 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3290 // struct _super_message_ref_t*, ...)
3291 MessageSendSuper2StretFixupFn =
3292 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3293 Params,
3294 true),
3295 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003296
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003297}
3298
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003299llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3300 FinishNonFragileABIModule();
3301
3302 return NULL;
3303}
3304
3305void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3306 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003307
3308 // Build list of all implemented classe addresses in array
3309 // L_OBJC_LABEL_CLASS_$.
3310 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3311 // list of 'nonlazy' implementations (defined as those with a +load{}
3312 // method!!).
3313 unsigned NumClasses = DefinedClasses.size();
3314 if (NumClasses) {
3315 std::vector<llvm::Constant*> Symbols(NumClasses);
3316 for (unsigned i=0; i<NumClasses; i++)
3317 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3318 ObjCTypes.Int8PtrTy);
3319 llvm::Constant* Init =
3320 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3321 NumClasses),
3322 Symbols);
3323
3324 llvm::GlobalVariable *GV =
3325 new llvm::GlobalVariable(Init->getType(), false,
3326 llvm::GlobalValue::InternalLinkage,
3327 Init,
3328 "\01L_OBJC_LABEL_CLASS_$",
3329 &CGM.getModule());
3330 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3331 UsedGlobals.push_back(GV);
3332 }
3333
3334 // Build list of all implemented category addresses in array
3335 // L_OBJC_LABEL_CATEGORY_$.
3336 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3337 // list of 'nonlazy' category implementations (defined as those with a +load{}
3338 // method!!).
3339 unsigned NumCategory = DefinedCategories.size();
3340 if (NumCategory) {
3341 std::vector<llvm::Constant*> Symbols(NumCategory);
3342 for (unsigned i=0; i<NumCategory; i++)
3343 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3344 ObjCTypes.Int8PtrTy);
3345 llvm::Constant* Init =
3346 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3347 NumCategory),
3348 Symbols);
3349
3350 llvm::GlobalVariable *GV =
3351 new llvm::GlobalVariable(Init->getType(), false,
3352 llvm::GlobalValue::InternalLinkage,
3353 Init,
3354 "\01L_OBJC_LABEL_CATEGORY_$",
3355 &CGM.getModule());
3356 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3357 UsedGlobals.push_back(GV);
3358 }
3359
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003360 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3361 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3362 std::vector<llvm::Constant*> Values(2);
3363 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3364 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3365 llvm::Constant* Init = llvm::ConstantArray::get(
3366 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3367 Values);
3368 llvm::GlobalVariable *IMGV =
3369 new llvm::GlobalVariable(Init->getType(), false,
3370 llvm::GlobalValue::InternalLinkage,
3371 Init,
3372 "\01L_OBJC_IMAGE_INFO",
3373 &CGM.getModule());
3374 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3375 UsedGlobals.push_back(IMGV);
3376
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003377 std::vector<llvm::Constant*> Used;
3378 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3379 e = UsedGlobals.end(); i != e; ++i) {
3380 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3381 }
3382
3383 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3384 llvm::GlobalValue *GV =
3385 new llvm::GlobalVariable(AT, false,
3386 llvm::GlobalValue::AppendingLinkage,
3387 llvm::ConstantArray::get(AT, Used),
3388 "llvm.used",
3389 &CGM.getModule());
3390
3391 GV->setSection("llvm.metadata");
3392
3393}
3394
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003395// Metadata flags
3396enum MetaDataDlags {
3397 CLS = 0x0,
3398 CLS_META = 0x1,
3399 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003400 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003401 CLS_EXCEPTION = 0x20
3402};
3403/// BuildClassRoTInitializer - generate meta-data for:
3404/// struct _class_ro_t {
3405/// uint32_t const flags;
3406/// uint32_t const instanceStart;
3407/// uint32_t const instanceSize;
3408/// uint32_t const reserved; // only when building for 64bit targets
3409/// const uint8_t * const ivarLayout;
3410/// const char *const name;
3411/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003412/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003413/// const struct _ivar_list_t *const ivars;
3414/// const uint8_t * const weakIvarLayout;
3415/// const struct _prop_list_t * const properties;
3416/// }
3417///
3418llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3419 unsigned flags,
3420 unsigned InstanceStart,
3421 unsigned InstanceSize,
3422 const ObjCImplementationDecl *ID) {
3423 std::string ClassName = ID->getNameAsString();
3424 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3425 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3426 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3427 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3428 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003429 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003430 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3431 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003432 // const struct _method_list_t * const baseMethods;
3433 std::vector<llvm::Constant*> Methods;
3434 std::string MethodListName("\01l_OBJC_$_");
3435 if (flags & CLS_META) {
3436 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3437 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3438 e = ID->classmeth_end(); i != e; ++i) {
3439 // Class methods should always be defined.
3440 Methods.push_back(GetMethodConstant(*i));
3441 }
3442 } else {
3443 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3444 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3445 e = ID->instmeth_end(); i != e; ++i) {
3446 // Instance methods should always be defined.
3447 Methods.push_back(GetMethodConstant(*i));
3448 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003449 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3450 e = ID->propimpl_end(); i != e; ++i) {
3451 ObjCPropertyImplDecl *PID = *i;
3452
3453 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3454 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3455
3456 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3457 if (llvm::Constant *C = GetMethodConstant(MD))
3458 Methods.push_back(C);
3459 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3460 if (llvm::Constant *C = GetMethodConstant(MD))
3461 Methods.push_back(C);
3462 }
3463 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003464 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003465 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003466 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003467
3468 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3469 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3470 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3471 + OID->getNameAsString(),
3472 OID->protocol_begin(),
3473 OID->protocol_end());
3474
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003475 if (flags & CLS_META)
3476 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3477 else
3478 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003479 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003480 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003481 if (flags & CLS_META)
3482 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3483 else
3484 Values[ 9] =
3485 EmitPropertyList(
3486 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3487 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003488 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3489 Values);
3490 llvm::GlobalVariable *CLASS_RO_GV =
3491 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3492 llvm::GlobalValue::InternalLinkage,
3493 Init,
3494 (flags & CLS_META) ?
3495 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3496 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3497 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003498 CLASS_RO_GV->setAlignment(
3499 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003500 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003501 UsedGlobals.push_back(CLASS_RO_GV);
3502 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003503
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003504}
3505
3506/// BuildClassMetaData - This routine defines that to-level meta-data
3507/// for the given ClassName for:
3508/// struct _class_t {
3509/// struct _class_t *isa;
3510/// struct _class_t * const superclass;
3511/// void *cache;
3512/// IMP *vtable;
3513/// struct class_ro_t *ro;
3514/// }
3515///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003516llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3517 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003518 llvm::Constant *IsAGV,
3519 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003520 llvm::Constant *ClassRoGV,
3521 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003522 std::vector<llvm::Constant*> Values(5);
3523 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003524 Values[1] = SuperClassGV
3525 ? SuperClassGV
3526 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003527 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3528 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3529 Values[4] = ClassRoGV; // &CLASS_RO_GV
3530 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3531 Values);
3532 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3533 if (GV)
3534 GV->setInitializer(Init);
3535 else
3536 GV =
3537 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3538 llvm::GlobalValue::ExternalLinkage,
3539 Init,
3540 ClassName,
3541 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003542 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003543 GV->setAlignment(
3544 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003545 if (HiddenVisibility)
3546 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003547 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003548 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003549}
3550
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003551void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3552 std::string ClassName = ID->getNameAsString();
3553 if (!ObjCEmptyCacheVar) {
3554 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3555 ObjCTypes.CachePtrTy,
3556 false,
3557 llvm::GlobalValue::ExternalLinkage,
3558 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003559 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003560 &CGM.getModule());
3561 UsedGlobals.push_back(ObjCEmptyCacheVar);
3562
3563 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3564 llvm::PointerType::getUnqual(
3565 ObjCTypes.ImpnfABITy),
3566 false,
3567 llvm::GlobalValue::ExternalLinkage,
3568 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003569 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003570 &CGM.getModule());
3571 UsedGlobals.push_back(ObjCEmptyVtableVar);
3572 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003573 assert(ID->getClassInterface() &&
3574 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003575 uint32_t InstanceStart =
3576 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3577 uint32_t InstanceSize = InstanceStart;
3578 uint32_t flags = CLS_META;
3579 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3580 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003581
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003582 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003583
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003584 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3585 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003586 flags |= OBJC2_CLS_HIDDEN;
3587 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003588 // class is root
3589 flags |= CLS_ROOT;
3590 std::string SuperClassName = ObjCClassName + ClassName;
3591 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3592 if (!SuperClassGV)
3593 SuperClassGV =
3594 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3595 llvm::GlobalValue::ExternalLinkage,
3596 0,
3597 SuperClassName,
3598 &CGM.getModule());
3599 UsedGlobals.push_back(SuperClassGV);
3600 std::string IsAClassName = ObjCMetaClassName + ClassName;
3601 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3602 if (!IsAGV)
3603 IsAGV =
3604 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3605 llvm::GlobalValue::ExternalLinkage,
3606 0,
3607 IsAClassName,
3608 &CGM.getModule());
3609 UsedGlobals.push_back(IsAGV);
3610 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003611 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003612 std::string RootClassName =
3613 ID->getClassInterface()->getSuperClass()->getNameAsString();
3614 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3615 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3616 if (!SuperClassGV)
3617 SuperClassGV =
3618 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3619 llvm::GlobalValue::ExternalLinkage,
3620 0,
3621 SuperClassName,
3622 &CGM.getModule());
3623 UsedGlobals.push_back(SuperClassGV);
3624 IsAGV = SuperClassGV;
3625 }
3626 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3627 InstanceStart,
3628 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003629 std::string TClassName = ObjCMetaClassName + ClassName;
3630 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003631 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3632 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003633
3634 // Metadata for the class
3635 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003636 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003637 flags |= OBJC2_CLS_HIDDEN;
3638 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003639 flags |= CLS_ROOT;
3640 SuperClassGV = 0;
3641 }
3642 else {
3643 // Has a root. Current class is not a root.
3644 std::string RootClassName =
3645 ID->getClassInterface()->getSuperClass()->getNameAsString();
3646 std::string SuperClassName = ObjCClassName + RootClassName;
3647 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3648 if (!SuperClassGV)
3649 SuperClassGV =
3650 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3651 llvm::GlobalValue::ExternalLinkage,
3652 0,
3653 SuperClassName,
3654 &CGM.getModule());
3655 UsedGlobals.push_back(SuperClassGV);
3656
3657 }
3658
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003659 InstanceStart = InstanceSize = 0;
3660 if (ObjCInterfaceDecl *OID =
3661 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3662 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003663 const llvm::Type *InterfaceTy =
3664 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3665 const llvm::StructLayout *Layout =
3666 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003667
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003668 RecordDecl::field_iterator firstField, lastField;
3669 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003670
3671 for (RecordDecl::field_iterator e = RD->field_end(),
3672 ifield = firstField; ifield != e; ++ifield)
3673 lastField = ifield;
3674
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003675 if (lastField != RD->field_end()) {
3676 FieldDecl *Field = *lastField;
3677 const llvm::Type *FieldTy =
3678 CGM.getTypes().ConvertTypeForMem(Field->getType());
3679 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3680 InstanceSize = Layout->getElementOffset(
3681 CGM.getTypes().getLLVMFieldNo(Field)) +
3682 Size;
3683 if (firstField == RD->field_end())
3684 InstanceStart = InstanceSize;
3685 else
3686 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3687 getLLVMFieldNo(*firstField));
3688 }
3689 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003690 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003691 InstanceStart,
3692 InstanceSize,
3693 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003694
3695 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003696 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003697 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3698 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003699 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003700}
3701
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003702/// GenerateProtocolRef - This routine is called to generate code for
3703/// a protocol reference expression; as in:
3704/// @code
3705/// @protocol(Proto1);
3706/// @endcode
3707/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3708/// which will hold address of the protocol meta-data.
3709///
3710llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3711 const ObjCProtocolDecl *PD) {
3712
3713 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3714 ObjCTypes.ExternalProtocolPtrTy);
3715
3716 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3717 ProtocolName += PD->getNameAsCString();
3718
3719 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3720 if (PTGV)
3721 return Builder.CreateLoad(PTGV, false, "tmp");
3722 PTGV = new llvm::GlobalVariable(
3723 Init->getType(), false,
3724 llvm::GlobalValue::WeakLinkage,
3725 Init,
3726 ProtocolName,
3727 &CGM.getModule());
3728 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3729 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3730 UsedGlobals.push_back(PTGV);
3731 return Builder.CreateLoad(PTGV, false, "tmp");
3732}
3733
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003734/// GenerateCategory - Build metadata for a category implementation.
3735/// struct _category_t {
3736/// const char * const name;
3737/// struct _class_t *const cls;
3738/// const struct _method_list_t * const instance_methods;
3739/// const struct _method_list_t * const class_methods;
3740/// const struct _protocol_list_t * const protocols;
3741/// const struct _prop_list_t * const properties;
3742/// }
3743///
3744void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3745{
3746 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003747 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3748 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003749 "_$_" + OCD->getNameAsString());
3750 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3751
3752 std::vector<llvm::Constant*> Values(6);
3753 Values[0] = GetClassName(OCD->getIdentifier());
3754 // meta-class entry symbol
3755 llvm::GlobalVariable *ClassGV =
3756 CGM.getModule().getGlobalVariable(ExtClassName);
3757 if (!ClassGV)
3758 ClassGV =
3759 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3760 llvm::GlobalValue::ExternalLinkage,
3761 0,
3762 ExtClassName,
3763 &CGM.getModule());
3764 UsedGlobals.push_back(ClassGV);
3765 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003766 std::vector<llvm::Constant*> Methods;
3767 std::string MethodListName(Prefix);
3768 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3769 "_$_" + OCD->getNameAsString();
3770
3771 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3772 e = OCD->instmeth_end(); i != e; ++i) {
3773 // Instance methods should always be defined.
3774 Methods.push_back(GetMethodConstant(*i));
3775 }
3776
3777 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003778 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003779 Methods);
3780
3781 MethodListName = Prefix;
3782 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3783 OCD->getNameAsString();
3784 Methods.clear();
3785 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3786 e = OCD->classmeth_end(); i != e; ++i) {
3787 // Class methods should always be defined.
3788 Methods.push_back(GetMethodConstant(*i));
3789 }
3790
3791 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003792 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003793 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003794 const ObjCCategoryDecl *Category =
3795 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003796 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3797 + Interface->getNameAsString() + "_$_"
3798 + Category->getNameAsString(),
3799 Category->protocol_begin(),
3800 Category->protocol_end());
3801
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003802 std::string ExtName(Interface->getNameAsString() + "_$_" +
3803 OCD->getNameAsString());
3804 Values[5] =
3805 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3806 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003807 llvm::Constant *Init =
3808 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3809 Values);
3810 llvm::GlobalVariable *GCATV
3811 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3812 false,
3813 llvm::GlobalValue::InternalLinkage,
3814 Init,
3815 ExtCatName,
3816 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003817 GCATV->setAlignment(
3818 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003819 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003820 UsedGlobals.push_back(GCATV);
3821 DefinedCategories.push_back(GCATV);
3822}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003823
3824/// GetMethodConstant - Return a struct objc_method constant for the
3825/// given method if it has been defined. The result is null if the
3826/// method has not been defined. The return value has type MethodPtrTy.
3827llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3828 const ObjCMethodDecl *MD) {
3829 // FIXME: Use DenseMap::lookup
3830 llvm::Function *Fn = MethodDefinitions[MD];
3831 if (!Fn)
3832 return 0;
3833
3834 std::vector<llvm::Constant*> Method(3);
3835 Method[0] =
3836 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3837 ObjCTypes.SelectorPtrTy);
3838 Method[1] = GetMethodVarType(MD);
3839 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3840 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3841}
3842
3843/// EmitMethodList - Build meta-data for method declarations
3844/// struct _method_list_t {
3845/// uint32_t entsize; // sizeof(struct _objc_method)
3846/// uint32_t method_count;
3847/// struct _objc_method method_list[method_count];
3848/// }
3849///
3850llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3851 const std::string &Name,
3852 const char *Section,
3853 const ConstantVector &Methods) {
3854 // Return null for empty list.
3855 if (Methods.empty())
3856 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3857
3858 std::vector<llvm::Constant*> Values(3);
3859 // sizeof(struct _objc_method)
3860 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3861 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3862 // method_count
3863 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3864 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3865 Methods.size());
3866 Values[2] = llvm::ConstantArray::get(AT, Methods);
3867 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3868
3869 llvm::GlobalVariable *GV =
3870 new llvm::GlobalVariable(Init->getType(), false,
3871 llvm::GlobalValue::InternalLinkage,
3872 Init,
3873 Name,
3874 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003875 GV->setAlignment(
3876 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003877 GV->setSection(Section);
3878 UsedGlobals.push_back(GV);
3879 return llvm::ConstantExpr::getBitCast(GV,
3880 ObjCTypes.MethodListnfABIPtrTy);
3881}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003882
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003883llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3884 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003885 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003886 unsigned long int Offset) {
3887
3888 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003889 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003890 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003891
3892 llvm::GlobalVariable *IvarOffsetGV =
3893 CGM.getModule().getGlobalVariable(ExternalName);
3894 if (IvarOffsetGV) {
3895 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003896 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003897 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003898 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003899 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003900 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003901 return IvarOffsetGV;
3902 }
3903
3904 IvarOffsetGV =
3905 new llvm::GlobalVariable(Init->getType(),
3906 false,
3907 llvm::GlobalValue::ExternalLinkage,
3908 Init,
3909 ExternalName,
3910 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003911 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003912 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003913 // @private and @package have hidden visibility.
3914 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3915 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3916 if (!globalVisibility)
3917 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003918 else
3919 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3920 if (IsClassHidden(OID))
3921 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3922
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003923 IvarOffsetGV->setSection("__DATA, __objc_const");
3924 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003925 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003926}
3927
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003928/// EmitIvarList - Emit the ivar list for the given
3929/// implementation. If ForClass is true the list of class ivars
3930/// (i.e. metaclass ivars) is emitted, otherwise the list of
3931/// interface ivars will be emitted. The return value has type
3932/// IvarListnfABIPtrTy.
3933/// struct _ivar_t {
3934/// unsigned long int *offset; // pointer to ivar offset location
3935/// char *name;
3936/// char *type;
3937/// uint32_t alignment;
3938/// uint32_t size;
3939/// }
3940/// struct _ivar_list_t {
3941/// uint32 entsize; // sizeof(struct _ivar_t)
3942/// uint32 count;
3943/// struct _iver_t list[count];
3944/// }
3945///
3946llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3947 const ObjCImplementationDecl *ID) {
3948
3949 std::vector<llvm::Constant*> Ivars, Ivar(5);
3950
3951 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3952 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3953
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003954 // FIXME. Consolidate this with similar code in GenerateClass.
3955 const llvm::Type *InterfaceTy =
3956 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3957 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003958 const llvm::StructLayout *Layout =
3959 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003960
3961 RecordDecl::field_iterator i,p;
3962 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003963 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3964
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003965 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003966 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003967 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3968 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003969 const ObjCIvarDecl *ivarDecl = *I++;
3970 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003971 if (Field->getIdentifier())
3972 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3973 else
3974 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3975 std::string TypeStr;
3976 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3977 Ivar[2] = GetMethodVarType(TypeStr);
3978 const llvm::Type *FieldTy =
3979 CGM.getTypes().ConvertTypeForMem(Field->getType());
3980 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3981 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3982 Field->getType().getTypePtr()) >> 3;
3983 Align = llvm::Log2_32(Align);
3984 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003985 // NOTE. Size of a bitfield does not match gcc's, because of the way
3986 // bitfields are treated special in each. But I am told that 'size'
3987 // for bitfield ivars is ignored by the runtime so it does not matter.
3988 // (even if it matters, some day, there is enough info. to get the bitfield
3989 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003990 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3991 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3992 }
3993 // Return null for empty list.
3994 if (Ivars.empty())
3995 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3996 std::vector<llvm::Constant*> Values(3);
3997 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3998 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3999 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4000 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4001 Ivars.size());
4002 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4003 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4004 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4005 llvm::GlobalVariable *GV =
4006 new llvm::GlobalVariable(Init->getType(), false,
4007 llvm::GlobalValue::InternalLinkage,
4008 Init,
4009 Prefix + OID->getNameAsString(),
4010 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004011 GV->setAlignment(
4012 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004013 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004014
4015 UsedGlobals.push_back(GV);
4016 return llvm::ConstantExpr::getBitCast(GV,
4017 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004018}
4019
4020llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4021 const ObjCProtocolDecl *PD) {
4022 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4023
4024 if (!Entry) {
4025 // We use the initializer as a marker of whether this is a forward
4026 // reference or not. At module finalization we add the empty
4027 // contents for protocols which were referenced but never defined.
4028 Entry =
4029 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4030 llvm::GlobalValue::ExternalLinkage,
4031 0,
4032 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4033 &CGM.getModule());
4034 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4035 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004036 }
4037
4038 return Entry;
4039}
4040
4041/// GetOrEmitProtocol - Generate the protocol meta-data:
4042/// @code
4043/// struct _protocol_t {
4044/// id isa; // NULL
4045/// const char * const protocol_name;
4046/// const struct _protocol_list_t * protocol_list; // super protocols
4047/// const struct method_list_t * const instance_methods;
4048/// const struct method_list_t * const class_methods;
4049/// const struct method_list_t *optionalInstanceMethods;
4050/// const struct method_list_t *optionalClassMethods;
4051/// const struct _prop_list_t * properties;
4052/// const uint32_t size; // sizeof(struct _protocol_t)
4053/// const uint32_t flags; // = 0
4054/// }
4055/// @endcode
4056///
4057
4058llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4059 const ObjCProtocolDecl *PD) {
4060 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4061
4062 // Early exit if a defining object has already been generated.
4063 if (Entry && Entry->hasInitializer())
4064 return Entry;
4065
4066 const char *ProtocolName = PD->getNameAsCString();
4067
4068 // Construct method lists.
4069 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4070 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4071 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4072 e = PD->instmeth_end(); i != e; ++i) {
4073 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004074 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004075 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4076 OptInstanceMethods.push_back(C);
4077 } else {
4078 InstanceMethods.push_back(C);
4079 }
4080 }
4081
4082 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4083 e = PD->classmeth_end(); i != e; ++i) {
4084 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004085 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004086 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4087 OptClassMethods.push_back(C);
4088 } else {
4089 ClassMethods.push_back(C);
4090 }
4091 }
4092
4093 std::vector<llvm::Constant*> Values(10);
4094 // isa is NULL
4095 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4096 Values[1] = GetClassName(PD->getIdentifier());
4097 Values[2] = EmitProtocolList(
4098 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4099 PD->protocol_begin(),
4100 PD->protocol_end());
4101
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004102 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004103 + PD->getNameAsString(),
4104 "__DATA, __objc_const",
4105 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004106 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004107 + PD->getNameAsString(),
4108 "__DATA, __objc_const",
4109 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004110 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004111 + PD->getNameAsString(),
4112 "__DATA, __objc_const",
4113 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004114 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004115 + PD->getNameAsString(),
4116 "__DATA, __objc_const",
4117 OptClassMethods);
4118 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4119 0, PD, ObjCTypes);
4120 uint32_t Size =
4121 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4122 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4123 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4124 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4125 Values);
4126
4127 if (Entry) {
4128 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004129 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004130 Entry->setInitializer(Init);
4131 } else {
4132 Entry =
4133 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004134 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004135 Init,
4136 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4137 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004138 Entry->setAlignment(
4139 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004140 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004141 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004142 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4143
4144 // Use this protocol meta-data to build protocol list table in section
4145 // __DATA, __objc_protolist
4146 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4147 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4148 ptype, false,
4149 llvm::GlobalValue::WeakLinkage,
4150 Entry,
4151 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4152 +ProtocolName,
4153 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004154 PTGV->setAlignment(
4155 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004156 PTGV->setSection("__DATA, __objc_protolist");
4157 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4158 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004159 return Entry;
4160}
4161
4162/// EmitProtocolList - Generate protocol list meta-data:
4163/// @code
4164/// struct _protocol_list_t {
4165/// long protocol_count; // Note, this is 32/64 bit
4166/// struct _protocol_t[protocol_count];
4167/// }
4168/// @endcode
4169///
4170llvm::Constant *
4171CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4172 ObjCProtocolDecl::protocol_iterator begin,
4173 ObjCProtocolDecl::protocol_iterator end) {
4174 std::vector<llvm::Constant*> ProtocolRefs;
4175
4176 for (; begin != end; ++begin)
4177 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4178
4179 // Just return null for empty protocol lists
4180 if (ProtocolRefs.empty())
4181 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4182
4183 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4184 if (GV)
4185 return GV;
4186 // This list is null terminated.
4187 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4188 ObjCTypes.ProtocolListnfABIPtrTy));
4189
4190 std::vector<llvm::Constant*> Values(2);
4191 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4192 Values[1] =
4193 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4194 ProtocolRefs.size()),
4195 ProtocolRefs);
4196
4197 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4198 GV = new llvm::GlobalVariable(Init->getType(), false,
4199 llvm::GlobalValue::InternalLinkage,
4200 Init,
4201 Name,
4202 &CGM.getModule());
4203 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004204 GV->setAlignment(
4205 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004206 UsedGlobals.push_back(GV);
4207 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4208}
4209
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004210/// GetMethodDescriptionConstant - This routine build following meta-data:
4211/// struct _objc_method {
4212/// SEL _cmd;
4213/// char *method_type;
4214/// char *_imp;
4215/// }
4216
4217llvm::Constant *
4218CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4219 std::vector<llvm::Constant*> Desc(3);
4220 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4221 ObjCTypes.SelectorPtrTy);
4222 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004223 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004224 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4225 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4226}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004227
4228/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4229/// This code gen. amounts to generating code for:
4230/// @code
4231/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4232/// @encode
4233///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004234LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004235 CodeGen::CodeGenFunction &CGF,
4236 QualType ObjectTy,
4237 llvm::Value *BaseValue,
4238 const ObjCIvarDecl *Ivar,
4239 const FieldDecl *Field,
4240 unsigned CVRQualifiers) {
4241 assert(ObjectTy->isObjCInterfaceType() &&
4242 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4243 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4244 // NOTE. This name must match one in EmitIvarOffsetVar.
4245 // FIXME. Consolidate into one naming routine.
4246 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
4247 + Ivar->getNameAsString());
4248
4249 llvm::GlobalVariable *IvarOffsetGV =
4250 CGM.getModule().getGlobalVariable(ExternalName);
4251 if (!IvarOffsetGV)
4252 IvarOffsetGV =
4253 new llvm::GlobalVariable(ObjCTypes.LongTy,
4254 false,
4255 llvm::GlobalValue::ExternalLinkage,
4256 0,
4257 ExternalName,
4258 &CGM.getModule());
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004259
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004260 // (char *) BaseValue
4261 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4262 ObjCTypes.Int8PtrTy);
4263 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4264 // (char*)BaseValue + Offset_symbol
4265 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4266 // (type *)((char*)BaseValue + Offset_symbol)
4267 const llvm::Type *IvarTy =
4268 CGM.getTypes().ConvertType(Ivar->getType());
4269 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4270 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004271
4272 if (Ivar->isBitField())
4273 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4274 CVRQualifiers);
4275
4276 LValue LV = LValue::MakeAddr(V,
4277 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4278 LValue::SetObjCIvar(LV, true);
4279 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004280}
4281
Fariborz Jahanian46551122009-02-04 00:22:57 +00004282CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4283 CodeGen::CodeGenFunction &CGF,
4284 QualType ResultType,
4285 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004286 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004287 QualType Arg0Ty,
4288 bool IsSuper,
4289 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004290 // FIXME. Even though IsSuper is passes. This function doese not
4291 // handle calls to 'super' receivers.
4292 CodeGenTypes &Types = CGM.getTypes();
4293 llvm::Value *Arg0 =
4294 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
4295
4296 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004297 // FIXME. This is too much work to get the ABI-specific result type
4298 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004299 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4300 llvm::SmallVector<QualType, 16>());
4301 llvm::Constant *Fn;
4302 std::string Name("\01l_");
4303 if (CGM.ReturnTypeUsesSret(FnInfo)) {
4304 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4305 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4306 // FIXME. Is there a better way of getting these names.
4307 // They are available in RuntimeFunctions vector pair.
4308 Name += "objc_msgSendId_stret_fixup";
4309 }
4310 else {
4311 Fn = ObjCTypes.MessageSendStretFixupFn;
4312 Name += "objc_msgSend_stret_fixup";
4313 }
4314 }
4315 else if (ResultType->isFloatingType()) {
4316 Fn = ObjCTypes.MessageSendFpretFixupFn;
4317 Name += "objc_msgSend_fpret_fixup";
4318 }
4319 else {
4320 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4321 Fn = ObjCTypes.MessageSendIdFixupFn;
4322 Name += "objc_msgSendId_fixup";
4323 }
4324 else {
4325 Fn = ObjCTypes.MessageSendFixupFn;
4326 Name += "objc_msgSend_fixup";
4327 }
4328 }
4329 Name += '_';
4330 std::string SelName(Sel.getAsString());
4331 // Replace all ':' in selector name with '_' ouch!
4332 for(unsigned i = 0; i < SelName.size(); i++)
4333 if (SelName[i] == ':')
4334 SelName[i] = '_';
4335 Name += SelName;
4336 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4337 if (!GV) {
4338 // Build messafe ref table entry.
4339 std::vector<llvm::Constant*> Values(2);
4340 Values[0] = Fn;
4341 Values[1] = GetMethodVarName(Sel);
4342 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4343 GV = new llvm::GlobalVariable(Init->getType(), false,
4344 llvm::GlobalValue::WeakLinkage,
4345 Init,
4346 Name,
4347 &CGM.getModule());
4348 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4349 GV->setAlignment(
4350 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4351 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4352 UsedGlobals.push_back(GV);
4353 }
4354 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004355
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004356 CallArgList ActualArgs;
4357 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4358 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4359 ObjCTypes.MessageRefCPtrTy));
4360 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004361 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4362 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4363 Callee = CGF.Builder.CreateLoad(Callee);
4364 const llvm::Type *T = llvm::PointerType::getUnqual(ObjCTypes.MessengerTy);
4365 T = llvm::PointerType::getUnqual(T);
4366 Callee = CGF.Builder.CreateBitCast(Callee, T);
4367 Callee = CGF.Builder.CreateLoad(Callee);
4368 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, false);
4369 Callee = CGF.Builder.CreateBitCast(Callee,
4370 llvm::PointerType::getUnqual(FTy));
4371 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004372}
4373
4374/// Generate code for a message send expression in the nonfragile abi.
4375CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4376 CodeGen::CodeGenFunction &CGF,
4377 QualType ResultType,
4378 Selector Sel,
4379 llvm::Value *Receiver,
4380 bool IsClassMessage,
4381 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004382 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004383 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004384 false, CallArgs);
4385}
4386
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004387/* *** */
4388
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004389CodeGen::CGObjCRuntime *
4390CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004391 return new CGObjCMac(CGM);
4392}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004393
4394CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004396 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004397}