blob: 6be50095546323407b3d609cc81673002deb8ee4 [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;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000104
105 /// ExceptionThrowFn - LLVM objc_exception_throw function.
106 llvm::Function *ExceptionThrowFn;
107
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000108 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
109 ~ObjCCommonTypesHelper(){}
110};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000111
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000112/// ObjCTypesHelper - Helper class that encapsulates lazy
113/// construction of varies types used during ObjC generation.
114class ObjCTypesHelper : public ObjCCommonTypesHelper {
115private:
116
117 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
118 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
119 *MessageSendSuperFpretFn;
120
121public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000122 /// SymtabTy - LLVM type for struct objc_symtab.
123 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000124 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
125 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000126 /// ModuleTy - LLVM type for struct objc_module.
127 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000128
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000129 /// ProtocolTy - LLVM type for struct objc_protocol.
130 const llvm::StructType *ProtocolTy;
131 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
132 const llvm::Type *ProtocolPtrTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension.
135 const llvm::StructType *ProtocolExtensionTy;
136 /// ProtocolExtensionTy - LLVM type for struct
137 /// objc_protocol_extension *.
138 const llvm::Type *ProtocolExtensionPtrTy;
139 /// MethodDescriptionTy - LLVM type for struct
140 /// objc_method_description.
141 const llvm::StructType *MethodDescriptionTy;
142 /// MethodDescriptionListTy - LLVM type for struct
143 /// objc_method_description_list.
144 const llvm::StructType *MethodDescriptionListTy;
145 /// MethodDescriptionListPtrTy - LLVM type for struct
146 /// objc_method_description_list *.
147 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000148 /// ProtocolListTy - LLVM type for struct objc_property_list.
149 const llvm::Type *ProtocolListTy;
150 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
151 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000152 /// CategoryTy - LLVM type for struct objc_category.
153 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000154 /// ClassTy - LLVM type for struct objc_class.
155 const llvm::StructType *ClassTy;
156 /// ClassPtrTy - LLVM type for struct objc_class *.
157 const llvm::Type *ClassPtrTy;
158 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
159 const llvm::StructType *ClassExtensionTy;
160 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
161 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000162 // IvarTy - LLVM type for struct objc_ivar.
163 const llvm::StructType *IvarTy;
164 /// IvarListTy - LLVM type for struct objc_ivar_list.
165 const llvm::Type *IvarListTy;
166 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
167 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000168 /// MethodListTy - LLVM type for struct objc_method_list.
169 const llvm::Type *MethodListTy;
170 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
171 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000172
173 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
174 const llvm::Type *ExceptionDataTy;
175
Anders Carlsson124526b2008-09-09 10:10:21 +0000176 /// 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
Daniel Dunbar948e2582009-02-15 07:36:20 +0000233 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
234 const llvm::Type *ProtocolnfABIPtrTy;
235
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000236 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
237 const llvm::StructType *ProtocolListnfABITy;
238
239 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
240 const llvm::Type *ProtocolListnfABIPtrTy;
241
242 // ClassnfABITy - LLVM for struct _class_t
243 const llvm::StructType *ClassnfABITy;
244
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000245 // ClassnfABIPtrTy - LLVM for struct _class_t*
246 const llvm::Type *ClassnfABIPtrTy;
247
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000248 // IvarnfABITy - LLVM for struct _ivar_t
249 const llvm::StructType *IvarnfABITy;
250
251 // IvarListnfABITy - LLVM for struct _ivar_list_t
252 const llvm::StructType *IvarListnfABITy;
253
254 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
255 const llvm::Type *IvarListnfABIPtrTy;
256
257 // ClassRonfABITy - LLVM for struct _class_ro_t
258 const llvm::StructType *ClassRonfABITy;
259
260 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
261 const llvm::Type *ImpnfABITy;
262
263 // CategorynfABITy - LLVM for struct _category_t
264 const llvm::StructType *CategorynfABITy;
265
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000266 // New types for nonfragile abi messaging.
267
268 // MessageRefTy - LLVM for:
269 // struct _message_ref_t {
270 // IMP messenger;
271 // SEL name;
272 // };
273 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000274 // MessageRefCTy - clang type for struct _message_ref_t
275 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000276
277 // MessageRefPtrTy - LLVM for struct _message_ref_t*
278 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000279 // MessageRefCPtrTy - clang type for struct _message_ref_t*
280 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000281
Fariborz Jahanianef163782009-02-05 01:13:09 +0000282 // MessengerTy - Type of the messenger (shown as IMP above)
283 const llvm::FunctionType *MessengerTy;
284
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000285 // SuperMessageRefTy - LLVM for:
286 // struct _super_message_ref_t {
287 // SUPER_IMP messenger;
288 // SEL name;
289 // };
290 const llvm::StructType *SuperMessageRefTy;
291
292 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
293 const llvm::Type *SuperMessageRefPtrTy;
294
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000295 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
296 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297};
298
299class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
300protected:
301 CodeGen::CodeGenModule &CGM;
302 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000303 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000304
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000305 /// LazySymbols - Symbols to generate a lazy reference for. See
306 /// DefinedSymbols and FinishModule().
307 std::set<IdentifierInfo*> LazySymbols;
308
309 /// DefinedSymbols - External symbols which are defined by this
310 /// module. The symbols in this list and LazySymbols are used to add
311 /// special linker symbols which ensure that Objective-C modules are
312 /// linked properly.
313 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000314
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000315 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000317
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000318 /// MethodVarNames - uniqued method variable names.
319 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000320
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000321 /// MethodVarTypes - uniqued method type signatures. We have to use
322 /// a StringMap here because have no other unique reference.
323 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000324
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000325 /// MethodDefinitions - map of methods which have been defined in
326 /// this translation unit.
327 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000328
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000329 /// PropertyNames - uniqued method variable names.
330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000331
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000332 /// ClassReferences - uniqued class references.
333 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000334
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000335 /// SelectorReferences - uniqued selector references.
336 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000337
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000338 /// Protocols - Protocols for which an objc_protocol structure has
339 /// been emitted. Forward declarations are handled by creating an
340 /// empty structure whose initializer is filled in when/if defined.
341 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000342
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000343 /// DefinedProtocols - Protocols which have actually been
344 /// defined. We should not need this, see FIXME in GenerateProtocol.
345 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000346
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000347 /// DefinedClasses - List of defined classes.
348 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000350 /// DefinedCategories - List of defined categories.
351 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000352
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000353 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000354 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000355 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000356
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000357 /// GetNameForMethod - Return a name for the given method.
358 /// \param[out] NameOut - The return value.
359 void GetNameForMethod(const ObjCMethodDecl *OMD,
360 const ObjCContainerDecl *CD,
361 std::string &NameOut);
362
363 /// GetMethodVarName - Return a unique constant for the given
364 /// selector's name. The return value has type char *.
365 llvm::Constant *GetMethodVarName(Selector Sel);
366 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
367 llvm::Constant *GetMethodVarName(const std::string &Name);
368
369 /// GetMethodVarType - Return a unique constant for the given
370 /// selector's name. The return value has type char *.
371
372 // FIXME: This is a horrible name.
373 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
374 llvm::Constant *GetMethodVarType(const std::string &Name);
375
376 /// GetPropertyName - Return a unique constant for the given
377 /// name. The return value has type char *.
378 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
379
380 // FIXME: This can be dropped once string functions are unified.
381 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
382 const Decl *Container);
383
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000384 /// GetClassName - Return a unique constant for the given selector's
385 /// name. The return value has type char *.
386 llvm::Constant *GetClassName(IdentifierInfo *Ident);
387
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000388 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
389 RecordDecl::field_iterator &FIV,
390 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000391 /// EmitPropertyList - Emit the given property list. The return
392 /// value has type PropertyListPtrTy.
393 llvm::Constant *EmitPropertyList(const std::string &Name,
394 const Decl *Container,
395 const ObjCContainerDecl *OCD,
396 const ObjCCommonTypesHelper &ObjCTypes);
397
Fariborz Jahanianda320092009-01-29 19:24:30 +0000398 /// GetProtocolRef - Return a reference to the internal protocol
399 /// description, creating an empty one if it has not been
400 /// defined. The return value has type ProtocolPtrTy.
401 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
402
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000403public:
404 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
405 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000406
407 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000408
409 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
410 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000411
412 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
413
414 /// GetOrEmitProtocol - Get the protocol object for the given
415 /// declaration, emitting it if necessary. The return value has type
416 /// ProtocolPtrTy.
417 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
418
419 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
420 /// object for the given declaration, emitting it if needed. These
421 /// forward references will be filled in with empty bodies if no
422 /// definition is seen. The return value has type ProtocolPtrTy.
423 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000424};
425
426class CGObjCMac : public CGObjCCommonMac {
427private:
428 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000429 /// EmitImageInfo - Emit the image info marker used to encode some module
430 /// level information.
431 void EmitImageInfo();
432
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000433 /// EmitModuleInfo - Another marker encoding module level
434 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000435 void EmitModuleInfo();
436
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000437 /// EmitModuleSymols - Emit module symbols, the list of defined
438 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000439 llvm::Constant *EmitModuleSymbols();
440
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000441 /// FinishModule - Write out global data structures at the end of
442 /// processing a translation unit.
443 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000444
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000445 /// EmitClassExtension - Generate the class extension structure used
446 /// to store the weak ivar layout and properties. The return value
447 /// has type ClassExtensionPtrTy.
448 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
449
450 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
451 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000452 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000453 const ObjCInterfaceDecl *ID);
454
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000455 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000456 QualType ResultType,
457 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000458 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000459 QualType Arg0Ty,
460 bool IsSuper,
461 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000462
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000463 /// EmitIvarList - Emit the ivar list for the given
464 /// implementation. If ForClass is true the list of class ivars
465 /// (i.e. metaclass ivars) is emitted, otherwise the list of
466 /// interface ivars will be emitted. The return value has type
467 /// IvarListPtrTy.
468 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000469 bool ForClass);
470
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000471 /// EmitMetaClass - Emit a forward reference to the class structure
472 /// for the metaclass of the given interface. The return value has
473 /// type ClassPtrTy.
474 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
475
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000477 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000478 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
479 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000480 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000481 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000482
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000483 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000484
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000485 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000486
487 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000488 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000489 llvm::Constant *EmitMethodList(const std::string &Name,
490 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000491 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000492
493 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000494 /// method declarations.
495 /// - TypeName: The name for the type containing the methods.
496 /// - IsProtocol: True iff these methods are for a protocol.
497 /// - ClassMethds: True iff these are class methods.
498 /// - Required: When true, only "required" methods are
499 /// listed. Similarly, when false only "optional" methods are
500 /// listed. For classes this should always be true.
501 /// - begin, end: The method list to output.
502 ///
503 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000504 llvm::Constant *EmitMethodDescList(const std::string &Name,
505 const char *Section,
506 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000508 /// GetOrEmitProtocol - Get the protocol object for the given
509 /// declaration, emitting it if necessary. The return value has type
510 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000511 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000512
513 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
514 /// object for the given declaration, emitting it if needed. These
515 /// forward references will be filled in with empty bodies if no
516 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000517 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000518
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// EmitProtocolExtension - Generate the protocol extension
520 /// structure used to store optional instance and class methods, and
521 /// protocol properties. The return value has type
522 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000523 llvm::Constant *
524 EmitProtocolExtension(const ObjCProtocolDecl *PD,
525 const ConstantVector &OptInstanceMethods,
526 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000527
528 /// EmitProtocolList - Generate the list of referenced
529 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000530 llvm::Constant *EmitProtocolList(const std::string &Name,
531 ObjCProtocolDecl::protocol_iterator begin,
532 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000533
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
535 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000536 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000537
Fariborz Jahanianda320092009-01-29 19:24:30 +0000538 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000539 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000540
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000541 virtual llvm::Function *ModuleInitFunction();
542
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000543 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000544 QualType ResultType,
545 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000546 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000547 bool IsClassMessage,
548 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000549
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000550 virtual CodeGen::RValue
551 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000552 QualType ResultType,
553 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000554 const ObjCInterfaceDecl *Class,
555 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000556 bool IsClassMessage,
557 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000560 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000561
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000562 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000563
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000564 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000565
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000566 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000567
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000568 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000569 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000570
Daniel Dunbar49f66022008-09-24 03:38:44 +0000571 virtual llvm::Function *GetPropertyGetFunction();
572 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000573 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000574
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000575 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
576 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000577 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
578 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000579 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000580 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000581 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
582 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000583 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
584 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000585 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
586 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000587 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
588 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000589
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000590 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
591 QualType ObjectTy,
592 llvm::Value *BaseValue,
593 const ObjCIvarDecl *Ivar,
594 const FieldDecl *Field,
595 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000596 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
597 ObjCInterfaceDecl *Interface,
598 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000599};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000600
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000601class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000602private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000603 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000604 llvm::GlobalVariable* ObjCEmptyCacheVar;
605 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000606 /// MetaClassReferences - uniqued meta class references.
607 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000608
609 /// FinishNonFragileABIModule - Write out global data structures at the end of
610 /// processing a translation unit.
611 void FinishNonFragileABIModule();
612
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000613 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
614 unsigned InstanceStart,
615 unsigned InstanceSize,
616 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000617 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
618 llvm::Constant *IsAGV,
619 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000620 llvm::Constant *ClassRoGV,
621 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000622
623 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
624
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000625 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
626
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000627 /// EmitMethodList - Emit the method list for the given
628 /// implementation. The return value has type MethodListnfABITy.
629 llvm::Constant *EmitMethodList(const std::string &Name,
630 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000631 const ConstantVector &Methods);
632 /// EmitIvarList - Emit the ivar list for the given
633 /// implementation. If ForClass is true the list of class ivars
634 /// (i.e. metaclass ivars) is emitted, otherwise the list of
635 /// interface ivars will be emitted. The return value has type
636 /// IvarListnfABIPtrTy.
637 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000638
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000639 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000640 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000641 unsigned long int offset);
642
Fariborz Jahanianda320092009-01-29 19:24:30 +0000643 /// GetOrEmitProtocol - Get the protocol object for the given
644 /// declaration, emitting it if necessary. The return value has type
645 /// ProtocolPtrTy.
646 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
647
648 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
649 /// object for the given declaration, emitting it if needed. These
650 /// forward references will be filled in with empty bodies if no
651 /// definition is seen. The return value has type ProtocolPtrTy.
652 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
653
654 /// EmitProtocolList - Generate the list of referenced
655 /// protocols. The return value has type ProtocolListPtrTy.
656 llvm::Constant *EmitProtocolList(const std::string &Name,
657 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000658 ObjCProtocolDecl::protocol_iterator end);
659
660 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
661 QualType ResultType,
662 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000663 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000664 QualType Arg0Ty,
665 bool IsSuper,
666 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000667
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000668 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
669 /// for the given class.
670 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000671 const ObjCInterfaceDecl *ID,
672 bool IsSuper = false);
673
674 /// EmitMetaClassRef - Return a Value * of the address of _class_t
675 /// meta-data
676 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
677 const ObjCInterfaceDecl *ID);
678
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000679 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
680 /// the given ivar.
681 ///
682 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000683 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000684 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000685
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000686 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
687 /// for the given selector.
688 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
689
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000690public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000691 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000692 // FIXME. All stubs for now!
693 virtual llvm::Function *ModuleInitFunction();
694
695 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
696 QualType ResultType,
697 Selector Sel,
698 llvm::Value *Receiver,
699 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000700 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000701
702 virtual CodeGen::RValue
703 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
704 QualType ResultType,
705 Selector Sel,
706 const ObjCInterfaceDecl *Class,
707 llvm::Value *Receiver,
708 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000709 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000710
711 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000712 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000713
714 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000715 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000716
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000717 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000718
719 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000720 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000721 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000722
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000723 virtual llvm::Function *GetPropertyGetFunction(){
724 return ObjCTypes.GetPropertyFn;
725 }
726 virtual llvm::Function *GetPropertySetFunction(){
727 return ObjCTypes.SetPropertyFn;
728 }
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000729 virtual llvm::Function *EnumerationMutationFunction() {
730 return ObjCTypes.EnumerationMutationFn;
731 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000732
733 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000734 const Stmt &S) {
735 CGF.ErrorUnsupported(&S, "try or synchronized statement");
736 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000737 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000738 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000739 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000740 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000741 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000742 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000743 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000744 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000745 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000746 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000747 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000748 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000749 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
750 QualType ObjectTy,
751 llvm::Value *BaseValue,
752 const ObjCIvarDecl *Ivar,
753 const FieldDecl *Field,
754 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000755 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
756 ObjCInterfaceDecl *Interface,
757 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000758};
759
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000760} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000761
762/* *** Helper Functions *** */
763
764/// getConstantGEP() - Help routine to construct simple GEPs.
765static llvm::Constant *getConstantGEP(llvm::Constant *C,
766 unsigned idx0,
767 unsigned idx1) {
768 llvm::Value *Idxs[] = {
769 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
770 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
771 };
772 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
773}
774
775/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000776
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000777CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
778 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000779{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000780 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000781 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000782}
783
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000784/// GetClass - Return a reference to the class for the given interface
785/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000786llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000787 const ObjCInterfaceDecl *ID) {
788 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000789}
790
791/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000792llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000793 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000794}
795
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000796/// Generate a constant CFString object.
797/*
798 struct __builtin_CFString {
799 const int *isa; // point to __CFConstantStringClassReference
800 int flags;
801 const char *str;
802 long length;
803 };
804*/
805
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000806llvm::Constant *CGObjCCommonMac::GenerateConstantString(
807 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000808 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000809}
810
811/// Generates a message send where the super is the receiver. This is
812/// a message send to self with special delivery semantics indicating
813/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000814CodeGen::RValue
815CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000816 QualType ResultType,
817 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000818 const ObjCInterfaceDecl *Class,
819 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000820 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000821 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000822 // Create and init a super structure; this is a (receiver, class)
823 // pair we will pass to objc_msgSendSuper.
824 llvm::Value *ObjCSuper =
825 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
826 llvm::Value *ReceiverAsObject =
827 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
828 CGF.Builder.CreateStore(ReceiverAsObject,
829 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000830
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000831 // If this is a class message the metaclass is passed as the target.
832 llvm::Value *Target;
833 if (IsClassMessage) {
834 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
835 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
836 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
837 Target = Super;
838 } else {
839 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
840 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000841 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
842 // and ObjCTypes types.
843 const llvm::Type *ClassTy =
844 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000845 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000846 CGF.Builder.CreateStore(Target,
847 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
848
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000849 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000850 ObjCSuper, ObjCTypes.SuperPtrCTy,
851 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000852}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000853
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000854/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000855CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000856 QualType ResultType,
857 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000858 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000859 bool IsClassMessage,
860 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000861 llvm::Value *Arg0 =
862 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000863 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000864 Arg0, CGF.getContext().getObjCIdType(),
865 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000866}
867
868CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000869 QualType ResultType,
870 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000871 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000872 QualType Arg0Ty,
873 bool IsSuper,
874 const CallArgList &CallArgs) {
875 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000876 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
877 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
878 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000879 CGF.getContext().getObjCSelType()));
880 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000881
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000882 CodeGenTypes &Types = CGM.getTypes();
883 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
884 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000885
886 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000887 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000888 Fn = ObjCTypes.getSendStretFn(IsSuper);
889 } else if (ResultType->isFloatingType()) {
890 // FIXME: Sadly, this is wrong. This actually depends on the
891 // architecture. This happens to be right for x86-32 though.
892 Fn = ObjCTypes.getSendFpretFn(IsSuper);
893 } else {
894 Fn = ObjCTypes.getSendFn(IsSuper);
895 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000896 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000897 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000898}
899
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000900llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000901 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000902 // FIXME: I don't understand why gcc generates this, or where it is
903 // resolved. Investigate. Its also wasteful to look this up over and
904 // over.
905 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
906
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000907 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
908 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000909}
910
Fariborz Jahanianda320092009-01-29 19:24:30 +0000911void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000912 // FIXME: We shouldn't need this, the protocol decl should contain
913 // enough information to tell us whether this was a declaration or a
914 // definition.
915 DefinedProtocols.insert(PD->getIdentifier());
916
917 // If we have generated a forward reference to this protocol, emit
918 // it now. Otherwise do nothing, the protocol objects are lazily
919 // emitted.
920 if (Protocols.count(PD->getIdentifier()))
921 GetOrEmitProtocol(PD);
922}
923
Fariborz Jahanianda320092009-01-29 19:24:30 +0000924llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000925 if (DefinedProtocols.count(PD->getIdentifier()))
926 return GetOrEmitProtocol(PD);
927 return GetOrEmitProtocolRef(PD);
928}
929
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000930/*
931 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
932 struct _objc_protocol {
933 struct _objc_protocol_extension *isa;
934 char *protocol_name;
935 struct _objc_protocol_list *protocol_list;
936 struct _objc__method_prototype_list *instance_methods;
937 struct _objc__method_prototype_list *class_methods
938 };
939
940 See EmitProtocolExtension().
941*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000942llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
943 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
944
945 // Early exit if a defining object has already been generated.
946 if (Entry && Entry->hasInitializer())
947 return Entry;
948
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000949 // FIXME: I don't understand why gcc generates this, or where it is
950 // resolved. Investigate. Its also wasteful to look this up over and
951 // over.
952 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
953
Chris Lattner8ec03f52008-11-24 03:54:41 +0000954 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000955
956 // Construct method lists.
957 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
958 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
959 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
960 e = PD->instmeth_end(); i != e; ++i) {
961 ObjCMethodDecl *MD = *i;
962 llvm::Constant *C = GetMethodDescriptionConstant(MD);
963 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
964 OptInstanceMethods.push_back(C);
965 } else {
966 InstanceMethods.push_back(C);
967 }
968 }
969
970 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
971 e = PD->classmeth_end(); i != e; ++i) {
972 ObjCMethodDecl *MD = *i;
973 llvm::Constant *C = GetMethodDescriptionConstant(MD);
974 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
975 OptClassMethods.push_back(C);
976 } else {
977 ClassMethods.push_back(C);
978 }
979 }
980
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000981 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000982 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000984 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000985 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000986 PD->protocol_begin(),
987 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000988 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000989 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
990 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000991 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
992 InstanceMethods);
993 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000994 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
995 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000996 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
997 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
999 Values);
1000
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001002 // Already created, fix the linkage and update the initializer.
1003 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001004 Entry->setInitializer(Init);
1005 } else {
1006 Entry =
1007 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1008 llvm::GlobalValue::InternalLinkage,
1009 Init,
1010 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1011 &CGM.getModule());
1012 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1013 UsedGlobals.push_back(Entry);
1014 // FIXME: Is this necessary? Why only for protocol?
1015 Entry->setAlignment(4);
1016 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001017
1018 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019}
1020
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001021llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1023
1024 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001025 // We use the initializer as a marker of whether this is a forward
1026 // reference or not. At module finalization we add the empty
1027 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001028 Entry =
1029 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001030 llvm::GlobalValue::ExternalLinkage,
1031 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001032 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001033 &CGM.getModule());
1034 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1035 UsedGlobals.push_back(Entry);
1036 // FIXME: Is this necessary? Why only for protocol?
1037 Entry->setAlignment(4);
1038 }
1039
1040 return Entry;
1041}
1042
1043/*
1044 struct _objc_protocol_extension {
1045 uint32_t size;
1046 struct objc_method_description_list *optional_instance_methods;
1047 struct objc_method_description_list *optional_class_methods;
1048 struct objc_property_list *instance_properties;
1049 };
1050*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001051llvm::Constant *
1052CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1053 const ConstantVector &OptInstanceMethods,
1054 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001055 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001056 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001057 std::vector<llvm::Constant*> Values(4);
1058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001059 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001060 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1061 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001062 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1063 OptInstanceMethods);
1064 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001065 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1066 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001067 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1068 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001069 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1070 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001071 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001074 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1075 Values[3]->isNullValue())
1076 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1077
1078 llvm::Constant *Init =
1079 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1080 llvm::GlobalVariable *GV =
1081 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1082 llvm::GlobalValue::InternalLinkage,
1083 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001084 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085 &CGM.getModule());
1086 // No special section, but goes in llvm.used
1087 UsedGlobals.push_back(GV);
1088
1089 return GV;
1090}
1091
1092/*
1093 struct objc_protocol_list {
1094 struct objc_protocol_list *next;
1095 long count;
1096 Protocol *list[];
1097 };
1098*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001099llvm::Constant *
1100CGObjCMac::EmitProtocolList(const std::string &Name,
1101 ObjCProtocolDecl::protocol_iterator begin,
1102 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001103 std::vector<llvm::Constant*> ProtocolRefs;
1104
Daniel Dunbardbc933702008-08-21 21:57:41 +00001105 for (; begin != end; ++begin)
1106 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107
1108 // Just return null for empty protocol lists
1109 if (ProtocolRefs.empty())
1110 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1111
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001112 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001113 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1114
1115 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001116 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001117 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1118 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1119 Values[2] =
1120 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1121 ProtocolRefs.size()),
1122 ProtocolRefs);
1123
1124 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1125 llvm::GlobalVariable *GV =
1126 new llvm::GlobalVariable(Init->getType(), false,
1127 llvm::GlobalValue::InternalLinkage,
1128 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001129 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 &CGM.getModule());
1131 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1132 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1133}
1134
1135/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001136 struct _objc_property {
1137 const char * const name;
1138 const char * const attributes;
1139 };
1140
1141 struct _objc_property_list {
1142 uint32_t entsize; // sizeof (struct _objc_property)
1143 uint32_t prop_count;
1144 struct _objc_property[prop_count];
1145 };
1146*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001147llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1148 const Decl *Container,
1149 const ObjCContainerDecl *OCD,
1150 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001151 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001152 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1153 E = OCD->prop_end(); I != E; ++I) {
1154 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001155 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001156 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001157 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1158 Prop));
1159 }
1160
1161 // Return null for empty list.
1162 if (Properties.empty())
1163 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1164
1165 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001166 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001167 std::vector<llvm::Constant*> Values(3);
1168 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1169 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1170 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1171 Properties.size());
1172 Values[2] = llvm::ConstantArray::get(AT, Properties);
1173 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1174
1175 llvm::GlobalVariable *GV =
1176 new llvm::GlobalVariable(Init->getType(), false,
1177 llvm::GlobalValue::InternalLinkage,
1178 Init,
1179 Name,
1180 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001181 if (ObjCABI == 2)
1182 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001183 // No special section on property lists?
1184 UsedGlobals.push_back(GV);
1185 return llvm::ConstantExpr::getBitCast(GV,
1186 ObjCTypes.PropertyListPtrTy);
1187
1188}
1189
1190/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191 struct objc_method_description_list {
1192 int count;
1193 struct objc_method_description list[];
1194 };
1195*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001196llvm::Constant *
1197CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1198 std::vector<llvm::Constant*> Desc(2);
1199 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1200 ObjCTypes.SelectorPtrTy);
1201 Desc[1] = GetMethodVarType(MD);
1202 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1203 Desc);
1204}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001205
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001206llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1207 const char *Section,
1208 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001209 // Return null for empty list.
1210 if (Methods.empty())
1211 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1212
1213 std::vector<llvm::Constant*> Values(2);
1214 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1215 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1216 Methods.size());
1217 Values[1] = llvm::ConstantArray::get(AT, Methods);
1218 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1219
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001220 llvm::GlobalVariable *GV =
1221 new llvm::GlobalVariable(Init->getType(), false,
1222 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001223 Init, Name, &CGM.getModule());
1224 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001225 UsedGlobals.push_back(GV);
1226 return llvm::ConstantExpr::getBitCast(GV,
1227 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001228}
1229
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001230/*
1231 struct _objc_category {
1232 char *category_name;
1233 char *class_name;
1234 struct _objc_method_list *instance_methods;
1235 struct _objc_method_list *class_methods;
1236 struct _objc_protocol_list *protocols;
1237 uint32_t size; // <rdar://4585769>
1238 struct _objc_property_list *instance_properties;
1239 };
1240 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001241void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001242 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001243
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001244 // FIXME: This is poor design, the OCD should have a pointer to the
1245 // category decl. Additionally, note that Category can be null for
1246 // the @implementation w/o an @interface case. Sema should just
1247 // create one for us as it does for @implementation so everyone else
1248 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001249 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001250 const ObjCCategoryDecl *Category =
1251 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001252 std::string ExtName(Interface->getNameAsString() + "_" +
1253 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001254
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001255 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1256 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1257 e = OCD->instmeth_end(); i != e; ++i) {
1258 // Instance methods should always be defined.
1259 InstanceMethods.push_back(GetMethodConstant(*i));
1260 }
1261 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1262 e = OCD->classmeth_end(); i != e; ++i) {
1263 // Class methods should always be defined.
1264 ClassMethods.push_back(GetMethodConstant(*i));
1265 }
1266
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001267 std::vector<llvm::Constant*> Values(7);
1268 Values[0] = GetClassName(OCD->getIdentifier());
1269 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001270 Values[2] =
1271 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1272 ExtName,
1273 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001274 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001275 Values[3] =
1276 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1277 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001278 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001279 if (Category) {
1280 Values[4] =
1281 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1282 Category->protocol_begin(),
1283 Category->protocol_end());
1284 } else {
1285 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1286 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001287 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001288
1289 // If there is no category @interface then there can be no properties.
1290 if (Category) {
1291 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001292 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001293 } else {
1294 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1295 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001296
1297 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1298 Values);
1299
1300 llvm::GlobalVariable *GV =
1301 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1302 llvm::GlobalValue::InternalLinkage,
1303 Init,
1304 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1305 &CGM.getModule());
1306 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1307 UsedGlobals.push_back(GV);
1308 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001309}
1310
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001311// FIXME: Get from somewhere?
1312enum ClassFlags {
1313 eClassFlags_Factory = 0x00001,
1314 eClassFlags_Meta = 0x00002,
1315 // <rdr://5142207>
1316 eClassFlags_HasCXXStructors = 0x02000,
1317 eClassFlags_Hidden = 0x20000,
1318 eClassFlags_ABI2_Hidden = 0x00010,
1319 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1320};
1321
1322// <rdr://5142207&4705298&4843145>
1323static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1324 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1325 // FIXME: Support -fvisibility
1326 switch (attr->getVisibility()) {
1327 default:
1328 assert(0 && "Unknown visibility");
1329 return false;
1330 case VisibilityAttr::DefaultVisibility:
1331 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1332 return false;
1333 case VisibilityAttr::HiddenVisibility:
1334 return true;
1335 }
1336 } else {
1337 return false; // FIXME: Support -fvisibility
1338 }
1339}
1340
1341/*
1342 struct _objc_class {
1343 Class isa;
1344 Class super_class;
1345 const char *name;
1346 long version;
1347 long info;
1348 long instance_size;
1349 struct _objc_ivar_list *ivars;
1350 struct _objc_method_list *methods;
1351 struct _objc_cache *cache;
1352 struct _objc_protocol_list *protocols;
1353 // Objective-C 1.0 extensions (<rdr://4585769>)
1354 const char *ivar_layout;
1355 struct _objc_class_ext *ext;
1356 };
1357
1358 See EmitClassExtension();
1359 */
1360void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001361 DefinedSymbols.insert(ID->getIdentifier());
1362
Chris Lattner8ec03f52008-11-24 03:54:41 +00001363 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001364 // FIXME: Gross
1365 ObjCInterfaceDecl *Interface =
1366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001367 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001368 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001369 Interface->protocol_begin(),
1370 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001371 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001372 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001373 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001374 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001375
1376 // FIXME: Set CXX-structors flag.
1377 if (IsClassHidden(ID->getClassInterface()))
1378 Flags |= eClassFlags_Hidden;
1379
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001380 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1381 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1382 e = ID->instmeth_end(); i != e; ++i) {
1383 // Instance methods should always be defined.
1384 InstanceMethods.push_back(GetMethodConstant(*i));
1385 }
1386 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1387 e = ID->classmeth_end(); i != e; ++i) {
1388 // Class methods should always be defined.
1389 ClassMethods.push_back(GetMethodConstant(*i));
1390 }
1391
1392 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1393 e = ID->propimpl_end(); i != e; ++i) {
1394 ObjCPropertyImplDecl *PID = *i;
1395
1396 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1397 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1398
1399 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1400 if (llvm::Constant *C = GetMethodConstant(MD))
1401 InstanceMethods.push_back(C);
1402 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1403 if (llvm::Constant *C = GetMethodConstant(MD))
1404 InstanceMethods.push_back(C);
1405 }
1406 }
1407
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001408 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001409 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001410 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001411 // Record a reference to the super class.
1412 LazySymbols.insert(Super->getIdentifier());
1413
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001414 Values[ 1] =
1415 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1416 ObjCTypes.ClassPtrTy);
1417 } else {
1418 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1419 }
1420 Values[ 2] = GetClassName(ID->getIdentifier());
1421 // Version is always 0.
1422 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1423 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1424 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001425 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001426 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001427 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001428 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001429 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001430 // cache is always NULL.
1431 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1432 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001433 // FIXME: Set ivar_layout
1434 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 Values[11] = EmitClassExtension(ID);
1436 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1437 Values);
1438
1439 llvm::GlobalVariable *GV =
1440 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1441 llvm::GlobalValue::InternalLinkage,
1442 Init,
1443 std::string("\01L_OBJC_CLASS_")+ClassName,
1444 &CGM.getModule());
1445 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1446 UsedGlobals.push_back(GV);
1447 // FIXME: Why?
1448 GV->setAlignment(32);
1449 DefinedClasses.push_back(GV);
1450}
1451
1452llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1453 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001454 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001455 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001456 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001457 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001458
1459 if (IsClassHidden(ID->getClassInterface()))
1460 Flags |= eClassFlags_Hidden;
1461
1462 std::vector<llvm::Constant*> Values(12);
1463 // The isa for the metaclass is the root of the hierarchy.
1464 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1465 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1466 Root = Super;
1467 Values[ 0] =
1468 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1469 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001470 // The super class for the metaclass is emitted as the name of the
1471 // super class. The runtime fixes this up to point to the
1472 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001473 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1474 Values[ 1] =
1475 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1476 ObjCTypes.ClassPtrTy);
1477 } else {
1478 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1479 }
1480 Values[ 2] = GetClassName(ID->getIdentifier());
1481 // Version is always 0.
1482 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1483 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1484 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001485 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001486 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001487 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001488 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001489 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001490 // cache is always NULL.
1491 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1492 Values[ 9] = Protocols;
1493 // ivar_layout for metaclass is always NULL.
1494 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1495 // The class extension is always unused for metaclasses.
1496 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1497 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1498 Values);
1499
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001500 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001501 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001502
1503 // Check for a forward reference.
1504 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1505 if (GV) {
1506 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1507 "Forward metaclass reference has incorrect type.");
1508 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1509 GV->setInitializer(Init);
1510 } else {
1511 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1512 llvm::GlobalValue::InternalLinkage,
1513 Init, Name,
1514 &CGM.getModule());
1515 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1517 UsedGlobals.push_back(GV);
1518 // FIXME: Why?
1519 GV->setAlignment(32);
1520
1521 return GV;
1522}
1523
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001524llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001525 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001526
1527 // FIXME: Should we look these up somewhere other than the
1528 // module. Its a bit silly since we only generate these while
1529 // processing an implementation, so exactly one pointer would work
1530 // if know when we entered/exitted an implementation block.
1531
1532 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001533 // Previously, metaclass with internal linkage may have been defined.
1534 // pass 'true' as 2nd argument so it is returned.
1535 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001536 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1537 "Forward metaclass reference has incorrect type.");
1538 return GV;
1539 } else {
1540 // Generate as an external reference to keep a consistent
1541 // module. This will be patched up when we emit the metaclass.
1542 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1543 llvm::GlobalValue::ExternalLinkage,
1544 0,
1545 Name,
1546 &CGM.getModule());
1547 }
1548}
1549
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001550/*
1551 struct objc_class_ext {
1552 uint32_t size;
1553 const char *weak_ivar_layout;
1554 struct _objc_property_list *properties;
1555 };
1556*/
1557llvm::Constant *
1558CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1559 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001560 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001561
1562 std::vector<llvm::Constant*> Values(3);
1563 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001564 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001565 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001566 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001567 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001568
1569 // Return null if no extension bits are used.
1570 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1571 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1572
1573 llvm::Constant *Init =
1574 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1575 llvm::GlobalVariable *GV =
1576 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1577 llvm::GlobalValue::InternalLinkage,
1578 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001579 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001580 &CGM.getModule());
1581 // No special section, but goes in llvm.used
1582 UsedGlobals.push_back(GV);
1583
1584 return GV;
1585}
1586
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001587/// countInheritedIvars - count number of ivars in class and its super class(s)
1588///
1589static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1590 int count = 0;
1591 if (!OI)
1592 return 0;
1593 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1594 if (SuperClass)
1595 count += countInheritedIvars(SuperClass);
1596 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1597 E = OI->ivar_end(); I != E; ++I)
1598 ++count;
1599 return count;
1600}
1601
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001602/// getInterfaceDeclForIvar - Get the interface declaration node where
1603/// this ivar is declared in.
1604/// FIXME. Ideally, this info should be in the ivar node. But currently
1605/// it is not and prevailing wisdom is that ASTs should not have more
1606/// info than is absolutely needed, even though this info reflects the
1607/// source language.
1608///
1609static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1610 const ObjCInterfaceDecl *OI,
1611 const ObjCIvarDecl *IVD) {
1612 if (!OI)
1613 return 0;
1614 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1615 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1616 E = OI->ivar_end(); I != E; ++I)
1617 if ((*I)->getIdentifier() == IVD->getIdentifier())
1618 return OI;
1619 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1620}
1621
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001622/*
1623 struct objc_ivar {
1624 char *ivar_name;
1625 char *ivar_type;
1626 int ivar_offset;
1627 };
1628
1629 struct objc_ivar_list {
1630 int ivar_count;
1631 struct objc_ivar list[count];
1632 };
1633 */
1634llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001635 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001636 std::vector<llvm::Constant*> Ivars, Ivar(3);
1637
1638 // When emitting the root class GCC emits ivar entries for the
1639 // actual class structure. It is not clear if we need to follow this
1640 // behavior; for now lets try and get away with not doing it. If so,
1641 // the cleanest solution would be to make up an ObjCInterfaceDecl
1642 // for the class.
1643 if (ForClass)
1644 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001645
1646 ObjCInterfaceDecl *OID =
1647 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1648 const llvm::Type *InterfaceTy =
1649 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001650 const llvm::StructLayout *Layout =
1651 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001652
1653 RecordDecl::field_iterator ifield, pfield;
1654 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001655 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1656 FieldDecl *Field = *ifield;
1657 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1658 getLLVMFieldNo(Field));
1659 if (Field->getIdentifier())
1660 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1661 else
1662 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001663 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001664 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001665 Ivar[1] = GetMethodVarType(TypeStr);
1666 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001667 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001668 }
1669
1670 // Return null for empty list.
1671 if (Ivars.empty())
1672 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1673
1674 std::vector<llvm::Constant*> Values(2);
1675 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1676 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1677 Ivars.size());
1678 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1679 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1680
1681 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1682 "\01L_OBJC_INSTANCE_VARIABLES_");
1683 llvm::GlobalVariable *GV =
1684 new llvm::GlobalVariable(Init->getType(), false,
1685 llvm::GlobalValue::InternalLinkage,
1686 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001687 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001688 &CGM.getModule());
1689 if (ForClass) {
1690 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1691 // FIXME: Why is this only here?
1692 GV->setAlignment(32);
1693 } else {
1694 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1695 }
1696 UsedGlobals.push_back(GV);
1697 return llvm::ConstantExpr::getBitCast(GV,
1698 ObjCTypes.IvarListPtrTy);
1699}
1700
1701/*
1702 struct objc_method {
1703 SEL method_name;
1704 char *method_types;
1705 void *method;
1706 };
1707
1708 struct objc_method_list {
1709 struct objc_method_list *obsolete;
1710 int count;
1711 struct objc_method methods_list[count];
1712 };
1713*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001714
1715/// GetMethodConstant - Return a struct objc_method constant for the
1716/// given method if it has been defined. The result is null if the
1717/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001718llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001719 // FIXME: Use DenseMap::lookup
1720 llvm::Function *Fn = MethodDefinitions[MD];
1721 if (!Fn)
1722 return 0;
1723
1724 std::vector<llvm::Constant*> Method(3);
1725 Method[0] =
1726 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1727 ObjCTypes.SelectorPtrTy);
1728 Method[1] = GetMethodVarType(MD);
1729 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1730 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1731}
1732
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001733llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1734 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001735 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001736 // Return null for empty list.
1737 if (Methods.empty())
1738 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1739
1740 std::vector<llvm::Constant*> Values(3);
1741 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1742 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1743 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1744 Methods.size());
1745 Values[2] = llvm::ConstantArray::get(AT, Methods);
1746 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1747
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001748 llvm::GlobalVariable *GV =
1749 new llvm::GlobalVariable(Init->getType(), false,
1750 llvm::GlobalValue::InternalLinkage,
1751 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001752 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001753 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001754 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001755 UsedGlobals.push_back(GV);
1756 return llvm::ConstantExpr::getBitCast(GV,
1757 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001758}
1759
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001760llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001761 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001762 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001763 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001764
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001765 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001766 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001767 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001768 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001769 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001770 llvm::GlobalValue::InternalLinkage,
1771 Name,
1772 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001773 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001774
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001775 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001776}
1777
1778llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001779 // Abuse this interface function as a place to finalize.
1780 FinishModule();
1781
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001782 return NULL;
1783}
1784
Daniel Dunbar49f66022008-09-24 03:38:44 +00001785llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1786 return ObjCTypes.GetPropertyFn;
1787}
1788
1789llvm::Function *CGObjCMac::GetPropertySetFunction() {
1790 return ObjCTypes.SetPropertyFn;
1791}
1792
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001793llvm::Function *CGObjCMac::EnumerationMutationFunction()
1794{
1795 return ObjCTypes.EnumerationMutationFn;
1796}
1797
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001798/*
1799
1800Objective-C setjmp-longjmp (sjlj) Exception Handling
1801--
1802
1803The basic framework for a @try-catch-finally is as follows:
1804{
1805 objc_exception_data d;
1806 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001807 bool _call_try_exit = true;
1808
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001809 objc_exception_try_enter(&d);
1810 if (!setjmp(d.jmp_buf)) {
1811 ... try body ...
1812 } else {
1813 // exception path
1814 id _caught = objc_exception_extract(&d);
1815
1816 // enter new try scope for handlers
1817 if (!setjmp(d.jmp_buf)) {
1818 ... match exception and execute catch blocks ...
1819
1820 // fell off end, rethrow.
1821 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001822 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001823 } else {
1824 // exception in catch block
1825 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001826 _call_try_exit = false;
1827 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001828 }
1829 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001830 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001831
1832finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001833 if (_call_try_exit)
1834 objc_exception_try_exit(&d);
1835
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001836 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001837 ... dispatch to finally destination ...
1838
1839finally_rethrow:
1840 objc_exception_throw(_rethrow);
1841
1842finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001843}
1844
1845This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001846uses _rethrow to determine if objc_exception_try_exit should be called
1847and if the object should be rethrown. This breaks in the face of
1848throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001849
1850We specialize this framework for a few particular circumstances:
1851
1852 - If there are no catch blocks, then we avoid emitting the second
1853 exception handling context.
1854
1855 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1856 e)) we avoid emitting the code to rethrow an uncaught exception.
1857
1858 - FIXME: If there is no @finally block we can do a few more
1859 simplifications.
1860
1861Rethrows and Jumps-Through-Finally
1862--
1863
1864Support for implicit rethrows and jumping through the finally block is
1865handled by storing the current exception-handling context in
1866ObjCEHStack.
1867
Daniel Dunbar898d5082008-09-30 01:06:03 +00001868In order to implement proper @finally semantics, we support one basic
1869mechanism for jumping through the finally block to an arbitrary
1870destination. Constructs which generate exits from a @try or @catch
1871block use this mechanism to implement the proper semantics by chaining
1872jumps, as necessary.
1873
1874This mechanism works like the one used for indirect goto: we
1875arbitrarily assign an ID to each destination and store the ID for the
1876destination in a variable prior to entering the finally block. At the
1877end of the finally block we simply create a switch to the proper
1878destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001879
1880Code gen for @synchronized(expr) stmt;
1881Effectively generating code for:
1882objc_sync_enter(expr);
1883@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001884*/
1885
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001886void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1887 const Stmt &S) {
1888 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001889 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001890 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001891 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001892 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1893 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1894 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001895
1896 // Push an EH context entry, used for handling rethrows and jumps
1897 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001898 CGF.PushCleanupBlock(FinallyBlock);
1899
Anders Carlsson273558f2009-02-07 21:37:21 +00001900 CGF.ObjCEHValueStack.push_back(0);
1901
Daniel Dunbar898d5082008-09-30 01:06:03 +00001902 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001903 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1904 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001905 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1906 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001907 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1908 "_call_try_exit");
1909 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1910
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001911 if (!isTry) {
1912 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001913 llvm::Value *Arg = CGF.EmitScalarExpr(
1914 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1915 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1916 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001917 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001918
1919 // Enter a new try block and call setjmp.
1920 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1921 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1922 "jmpbufarray");
1923 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1924 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1925 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001926
Daniel Dunbar55e87422008-11-11 02:29:29 +00001927 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1928 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001929 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001930 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001931
1932 // Emit the @try block.
1933 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001934 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1935 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001936 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001937
1938 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001939 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001940
1941 // Retrieve the exception object. We may emit multiple blocks but
1942 // nothing can cross this so the value is already in SSA form.
1943 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1944 ExceptionData,
1945 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001946 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001947 if (!isTry)
1948 {
1949 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001950 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001951 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001952 }
1953 else if (const ObjCAtCatchStmt* CatchStmt =
1954 cast<ObjCAtTryStmt>(S).getCatchStmts())
1955 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001956 // Enter a new exception try block (in case a @catch block throws
1957 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001958 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001959
Anders Carlsson80f25672008-09-09 17:59:25 +00001960 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1961 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001962 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001963
Daniel Dunbar55e87422008-11-11 02:29:29 +00001964 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1965 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001966 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001967
1968 CGF.EmitBlock(CatchBlock);
1969
Daniel Dunbar55e40722008-09-27 07:03:52 +00001970 // Handle catch list. As a special case we check if everything is
1971 // matched and avoid generating code for falling off the end if
1972 // so.
1973 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001974 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001975 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001976
Anders Carlssondde0a942008-09-11 09:15:33 +00001977 const DeclStmt *CatchParam =
1978 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001979 const VarDecl *VD = 0;
1980 const PointerType *PT = 0;
1981
Anders Carlsson80f25672008-09-09 17:59:25 +00001982 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001983 if (!CatchParam) {
1984 AllMatched = true;
1985 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001986 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001987 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001988
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001989 // catch(id e) always matches.
1990 // FIXME: For the time being we also match id<X>; this should
1991 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00001992 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001993 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001994 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001995 }
1996
Daniel Dunbar55e40722008-09-27 07:03:52 +00001997 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001998 if (CatchParam) {
1999 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002000 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002001 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002002 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002003
Anders Carlssondde0a942008-09-11 09:15:33 +00002004 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002005 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002006 break;
2007 }
2008
Daniel Dunbar129271a2008-09-27 07:36:24 +00002009 assert(PT && "Unexpected non-pointer type in @catch");
2010 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002011 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002012 assert(ObjCType && "Catch parameter must have Objective-C type!");
2013
2014 // Check if the @catch block matches the exception object.
2015 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2016
Anders Carlsson80f25672008-09-09 17:59:25 +00002017 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2018 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002019
Daniel Dunbar55e87422008-11-11 02:29:29 +00002020 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002021
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002022 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002023 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002024
2025 // Emit the @catch block.
2026 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002027 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002028 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002029
2030 llvm::Value *Tmp =
2031 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2032 "tmp");
2033 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002034
2035 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002036 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002037
2038 CGF.EmitBlock(NextCatchBlock);
2039 }
2040
Daniel Dunbar55e40722008-09-27 07:03:52 +00002041 if (!AllMatched) {
2042 // None of the handlers caught the exception, so store it to be
2043 // rethrown at the end of the @finally block.
2044 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002045 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002046 }
2047
2048 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002049 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002050 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2051 ExceptionData),
2052 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002053 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002054 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002055 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002056 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002057 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002058 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002059 }
2060
Daniel Dunbar898d5082008-09-30 01:06:03 +00002061 // Pop the exception-handling stack entry. It is important to do
2062 // this now, because the code in the @finally block is not in this
2063 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002064 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2065
Anders Carlsson273558f2009-02-07 21:37:21 +00002066 CGF.ObjCEHValueStack.pop_back();
2067
Anders Carlsson80f25672008-09-09 17:59:25 +00002068 // Emit the @finally block.
2069 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002070 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2071
2072 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2073
2074 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002075 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002076
2077 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002078 if (isTry) {
2079 if (const ObjCAtFinallyStmt* FinallyStmt =
2080 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2081 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2082 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002083 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002084 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002085 // For @synchronized, call objc_sync_enter(sync.expr)
2086 llvm::Value *Arg = CGF.EmitScalarExpr(
2087 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2088 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2089 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2090 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002091
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002092 // Emit the switch block
2093 if (Info.SwitchBlock)
2094 CGF.EmitBlock(Info.SwitchBlock);
2095 if (Info.EndBlock)
2096 CGF.EmitBlock(Info.EndBlock);
2097
Daniel Dunbar898d5082008-09-30 01:06:03 +00002098 CGF.EmitBlock(FinallyRethrow);
2099 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2100 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002101 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002102
2103 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002104}
2105
2106void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002107 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002108 llvm::Value *ExceptionAsObject;
2109
2110 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2111 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2112 ExceptionAsObject =
2113 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2114 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002115 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002116 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002117 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002118 }
2119
2120 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002121 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002122
2123 // Clear the insertion point to indicate we are in unreachable code.
2124 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002125}
2126
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002127/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002128/// object: objc_read_weak (id *src)
2129///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002130llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002131 llvm::Value *AddrWeakObj)
2132{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002133 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002134 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002135 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002136 return read_weak;
2137}
2138
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002139/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2140/// objc_assign_weak (id src, id *dst)
2141///
2142void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2143 llvm::Value *src, llvm::Value *dst)
2144{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002145 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2146 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002147 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2148 src, dst, "weakassign");
2149 return;
2150}
2151
Fariborz Jahanian58626502008-11-19 00:59:10 +00002152/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2153/// objc_assign_global (id src, id *dst)
2154///
2155void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2156 llvm::Value *src, llvm::Value *dst)
2157{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002158 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2159 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002160 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2161 src, dst, "globalassign");
2162 return;
2163}
2164
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002165/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2166/// objc_assign_ivar (id src, id *dst)
2167///
2168void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2169 llvm::Value *src, llvm::Value *dst)
2170{
2171 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2172 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2173 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2174 src, dst, "assignivar");
2175 return;
2176}
2177
Fariborz Jahanian58626502008-11-19 00:59:10 +00002178/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2179/// objc_assign_strongCast (id src, id *dst)
2180///
2181void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2182 llvm::Value *src, llvm::Value *dst)
2183{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002184 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2185 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002186 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2187 src, dst, "weakassign");
2188 return;
2189}
2190
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002191/// EmitObjCValueForIvar - Code Gen for ivar reference.
2192///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002193LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2194 QualType ObjectTy,
2195 llvm::Value *BaseValue,
2196 const ObjCIvarDecl *Ivar,
2197 const FieldDecl *Field,
2198 unsigned CVRQualifiers) {
2199 if (Ivar->isBitField())
2200 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2201 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002202 // TODO: Add a special case for isa (index 0)
2203 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2204 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002205 LValue LV = LValue::MakeAddr(V,
2206 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2207 LValue::SetObjCIvar(LV, true);
2208 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002209}
2210
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002211llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2212 ObjCInterfaceDecl *Interface,
2213 const ObjCIvarDecl *Ivar) {
2214 const llvm::Type *InterfaceLTy =
2215 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2216 const llvm::StructLayout *Layout =
2217 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2218 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2219 uint64_t Offset =
2220 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2221
2222 return llvm::ConstantInt::get(
2223 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2224 Offset);
2225}
2226
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002227/* *** Private Interface *** */
2228
2229/// EmitImageInfo - Emit the image info marker used to encode some module
2230/// level information.
2231///
2232/// See: <rdr://4810609&4810587&4810587>
2233/// struct IMAGE_INFO {
2234/// unsigned version;
2235/// unsigned flags;
2236/// };
2237enum ImageInfoFlags {
2238 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2239 eImageInfo_GarbageCollected = (1 << 1),
2240 eImageInfo_GCOnly = (1 << 2)
2241};
2242
2243void CGObjCMac::EmitImageInfo() {
2244 unsigned version = 0; // Version is unused?
2245 unsigned flags = 0;
2246
2247 // FIXME: Fix and continue?
2248 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2249 flags |= eImageInfo_GarbageCollected;
2250 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2251 flags |= eImageInfo_GCOnly;
2252
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002253 // Emitted as int[2];
2254 llvm::Constant *values[2] = {
2255 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2256 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2257 };
2258 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002259 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002260 new llvm::GlobalVariable(AT, true,
2261 llvm::GlobalValue::InternalLinkage,
2262 llvm::ConstantArray::get(AT, values, 2),
2263 "\01L_OBJC_IMAGE_INFO",
2264 &CGM.getModule());
2265
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002266 if (ObjCABI == 1) {
2267 GV->setSection("__OBJC, __image_info,regular");
2268 } else {
2269 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2270 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002271
2272 UsedGlobals.push_back(GV);
2273}
2274
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002275
2276// struct objc_module {
2277// unsigned long version;
2278// unsigned long size;
2279// const char *name;
2280// Symtab symtab;
2281// };
2282
2283// FIXME: Get from somewhere
2284static const int ModuleVersion = 7;
2285
2286void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002287 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002288
2289 std::vector<llvm::Constant*> Values(4);
2290 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2291 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002292 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002293 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002294 Values[3] = EmitModuleSymbols();
2295
2296 llvm::GlobalVariable *GV =
2297 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2298 llvm::GlobalValue::InternalLinkage,
2299 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2300 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002301 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002302 &CGM.getModule());
2303 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2304 UsedGlobals.push_back(GV);
2305}
2306
2307llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308 unsigned NumClasses = DefinedClasses.size();
2309 unsigned NumCategories = DefinedCategories.size();
2310
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002311 // Return null if no symbols were defined.
2312 if (!NumClasses && !NumCategories)
2313 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2314
2315 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2317 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2318 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2319 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2320
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002321 // The runtime expects exactly the list of defined classes followed
2322 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002324 for (unsigned i=0; i<NumClasses; i++)
2325 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2326 ObjCTypes.Int8PtrTy);
2327 for (unsigned i=0; i<NumCategories; i++)
2328 Symbols[NumClasses + i] =
2329 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2330 ObjCTypes.Int8PtrTy);
2331
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002333 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002334 NumClasses + NumCategories),
2335 Symbols);
2336
2337 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2338
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002339 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002340 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002341 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002343 "\01L_OBJC_SYMBOLS",
2344 &CGM.getModule());
2345 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2346 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002347 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2348}
2349
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002350llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002352 LazySymbols.insert(ID->getIdentifier());
2353
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2355
2356 if (!Entry) {
2357 llvm::Constant *Casted =
2358 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2359 ObjCTypes.ClassPtrTy);
2360 Entry =
2361 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2362 llvm::GlobalValue::InternalLinkage,
2363 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2364 &CGM.getModule());
2365 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2366 UsedGlobals.push_back(Entry);
2367 }
2368
2369 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002370}
2371
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002372llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002373 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2374
2375 if (!Entry) {
2376 llvm::Constant *Casted =
2377 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2378 ObjCTypes.SelectorPtrTy);
2379 Entry =
2380 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2381 llvm::GlobalValue::InternalLinkage,
2382 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2383 &CGM.getModule());
2384 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2385 UsedGlobals.push_back(Entry);
2386 }
2387
2388 return Builder.CreateLoad(Entry, false, "tmp");
2389}
2390
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002391llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002392 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002393
2394 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002395 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002396 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002397 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002398 llvm::GlobalValue::InternalLinkage,
2399 C, "\01L_OBJC_CLASS_NAME_",
2400 &CGM.getModule());
2401 Entry->setSection("__TEXT,__cstring,cstring_literals");
2402 UsedGlobals.push_back(Entry);
2403 }
2404
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002405 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002406}
2407
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002408llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002409 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2410
2411 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002412 // FIXME: Avoid std::string copying.
2413 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002414 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002415 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002416 llvm::GlobalValue::InternalLinkage,
2417 C, "\01L_OBJC_METH_VAR_NAME_",
2418 &CGM.getModule());
2419 Entry->setSection("__TEXT,__cstring,cstring_literals");
2420 UsedGlobals.push_back(Entry);
2421 }
2422
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002423 return getConstantGEP(Entry, 0, 0);
2424}
2425
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002427llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2429}
2430
2431// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002432llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2434}
2435
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002436llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002437 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002438
2439 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002441 Entry =
2442 new llvm::GlobalVariable(C->getType(), false,
2443 llvm::GlobalValue::InternalLinkage,
2444 C, "\01L_OBJC_METH_VAR_TYPE_",
2445 &CGM.getModule());
2446 Entry->setSection("__TEXT,__cstring,cstring_literals");
2447 UsedGlobals.push_back(Entry);
2448 }
2449
2450 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002451}
2452
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002454llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002455 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002456 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2457 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 return GetMethodVarType(TypeStr);
2459}
2460
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002461// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002462llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002463 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2464
2465 if (!Entry) {
2466 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2467 Entry =
2468 new llvm::GlobalVariable(C->getType(), false,
2469 llvm::GlobalValue::InternalLinkage,
2470 C, "\01L_OBJC_PROP_NAME_ATTR_",
2471 &CGM.getModule());
2472 Entry->setSection("__TEXT,__cstring,cstring_literals");
2473 UsedGlobals.push_back(Entry);
2474 }
2475
2476 return getConstantGEP(Entry, 0, 0);
2477}
2478
2479// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002480// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002481llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002482 const Decl *Container) {
2483 std::string TypeStr;
2484 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002485 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2486}
2487
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002488void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2489 const ObjCContainerDecl *CD,
2490 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002491 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002492 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002493 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002494 assert (CD && "Missing container decl in GetNameForMethod");
2495 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002496 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2497 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002498 NameOut += ' ';
2499 NameOut += D->getSelector().getAsString();
2500 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002501}
2502
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002503/// GetFirstIvarInRecord - This routine returns the record for the
2504/// implementation of the fiven class OID. It also returns field
2505/// corresponding to the first ivar in the class in FIV. It also
2506/// returns the one before the first ivar.
2507///
2508const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2509 const ObjCInterfaceDecl *OID,
2510 RecordDecl::field_iterator &FIV,
2511 RecordDecl::field_iterator &PIV) {
2512 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2513 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2514 RecordDecl::field_iterator ifield = RD->field_begin();
2515 RecordDecl::field_iterator pfield = RD->field_end();
2516 while (countSuperClassIvars-- > 0) {
2517 pfield = ifield;
2518 ++ifield;
2519 }
2520 FIV = ifield;
2521 PIV = pfield;
2522 return RD;
2523}
2524
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002525void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002526 EmitModuleInfo();
2527
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002528 // Emit the dummy bodies for any protocols which were referenced but
2529 // never defined.
2530 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2531 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2532 if (i->second->hasInitializer())
2533 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002534
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002535 std::vector<llvm::Constant*> Values(5);
2536 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2537 Values[1] = GetClassName(i->first);
2538 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2539 Values[3] = Values[4] =
2540 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2541 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2542 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2543 Values));
2544 }
2545
2546 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002547 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002548 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002549 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002550 }
2551
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002552 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002553 llvm::GlobalValue *GV =
2554 new llvm::GlobalVariable(AT, false,
2555 llvm::GlobalValue::AppendingLinkage,
2556 llvm::ConstantArray::get(AT, Used),
2557 "llvm.used",
2558 &CGM.getModule());
2559
2560 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002561
2562 // Add assembler directives to add lazy undefined symbol references
2563 // for classes which are referenced but not defined. This is
2564 // important for correct linker interaction.
2565
2566 // FIXME: Uh, this isn't particularly portable.
2567 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002568
2569 if (!CGM.getModule().getModuleInlineAsm().empty())
2570 s << "\n";
2571
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002572 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2573 e = LazySymbols.end(); i != e; ++i) {
2574 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2575 }
2576 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2577 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002578 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002579 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2580 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002581
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002582 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002583}
2584
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002585CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002586 : CGObjCCommonMac(cgm),
2587 ObjCTypes(cgm)
2588{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002589 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002590 ObjCABI = 2;
2591}
2592
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002593/* *** */
2594
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002595ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2596: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002597{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002598 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2599 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002600
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002601 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002602 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002603 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002604 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2605
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002606 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002607 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002608 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002609
2610 // FIXME: It would be nice to unify this with the opaque type, so
2611 // that the IR comes out a bit cleaner.
2612 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2613 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002614
2615 // I'm not sure I like this. The implicit coordination is a bit
2616 // gross. We should solve this in a reasonable fashion because this
2617 // is a pretty common task (match some runtime data structure with
2618 // an LLVM data structure).
2619
2620 // FIXME: This is leaked.
2621 // FIXME: Merge with rewriter code?
2622
2623 // struct _objc_super {
2624 // id self;
2625 // Class cls;
2626 // }
2627 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2628 SourceLocation(),
2629 &Ctx.Idents.get("_objc_super"));
2630 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2631 Ctx.getObjCIdType(), 0, false));
2632 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2633 Ctx.getObjCClassType(), 0, false));
2634 RD->completeDefinition(Ctx);
2635
2636 SuperCTy = Ctx.getTagDeclType(RD);
2637 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2638
2639 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002640 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2641
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002642 // struct _prop_t {
2643 // char *name;
2644 // char *attributes;
2645 // }
2646 PropertyTy = llvm::StructType::get(Int8PtrTy,
2647 Int8PtrTy,
2648 NULL);
2649 CGM.getModule().addTypeName("struct._prop_t",
2650 PropertyTy);
2651
2652 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002653 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002654 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002655 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002656 // }
2657 PropertyListTy = llvm::StructType::get(IntTy,
2658 IntTy,
2659 llvm::ArrayType::get(PropertyTy, 0),
2660 NULL);
2661 CGM.getModule().addTypeName("struct._prop_list_t",
2662 PropertyListTy);
2663 // struct _prop_list_t *
2664 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2665
2666 // struct _objc_method {
2667 // SEL _cmd;
2668 // char *method_type;
2669 // char *_imp;
2670 // }
2671 MethodTy = llvm::StructType::get(SelectorPtrTy,
2672 Int8PtrTy,
2673 Int8PtrTy,
2674 NULL);
2675 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002676
2677 // struct _objc_cache *
2678 CacheTy = llvm::OpaqueType::get();
2679 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2680 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002681
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002682 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002683
2684 QualType IdType = Ctx.getObjCIdType();
2685 QualType SelType = Ctx.getObjCSelType();
2686 llvm::SmallVector<QualType,16> Params;
2687 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002688
2689 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002690 Params.push_back(IdType);
2691 Params.push_back(SelType);
2692 Params.push_back(Ctx.LongTy);
2693 Params.push_back(Ctx.BoolTy);
2694 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2695 false);
2696 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002697
2698 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2699 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002700 Params.push_back(IdType);
2701 Params.push_back(SelType);
2702 Params.push_back(Ctx.LongTy);
2703 Params.push_back(IdType);
2704 Params.push_back(Ctx.BoolTy);
2705 Params.push_back(Ctx.BoolTy);
2706 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2707 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2708
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002709 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002710
2711 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002712 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002713 Params.push_back(IdType);
2714 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2715 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2716 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002717
2718 // gc's API
2719 // id objc_read_weak (id *)
2720 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002721 Params.push_back(Ctx.getPointerType(IdType));
2722 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2723 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2724
2725 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002726 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002727 Params.push_back(IdType);
2728 Params.push_back(Ctx.getPointerType(IdType));
2729
2730 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2731 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2732 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2733 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2734 GcAssignStrongCastFn =
2735 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002736
2737 // void objc_exception_throw(id)
2738 Params.clear();
2739 Params.push_back(IdType);
2740
2741 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2742
2743 ExceptionThrowFn =
2744 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002745}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002746
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002747ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2748 : ObjCCommonTypesHelper(cgm)
2749{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002750 // struct _objc_method_description {
2751 // SEL name;
2752 // char *types;
2753 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002754 MethodDescriptionTy =
2755 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002756 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002757 NULL);
2758 CGM.getModule().addTypeName("struct._objc_method_description",
2759 MethodDescriptionTy);
2760
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002761 // struct _objc_method_description_list {
2762 // int count;
2763 // struct _objc_method_description[1];
2764 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002765 MethodDescriptionListTy =
2766 llvm::StructType::get(IntTy,
2767 llvm::ArrayType::get(MethodDescriptionTy, 0),
2768 NULL);
2769 CGM.getModule().addTypeName("struct._objc_method_description_list",
2770 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002771
2772 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002773 MethodDescriptionListPtrTy =
2774 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2775
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002776 // Protocol description structures
2777
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002778 // struct _objc_protocol_extension {
2779 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2780 // struct _objc_method_description_list *optional_instance_methods;
2781 // struct _objc_method_description_list *optional_class_methods;
2782 // struct _objc_property_list *instance_properties;
2783 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002784 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002785 llvm::StructType::get(IntTy,
2786 MethodDescriptionListPtrTy,
2787 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002788 PropertyListPtrTy,
2789 NULL);
2790 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2791 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002792
2793 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002794 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2795
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002796 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002797
2798 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2799 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2800
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002801 const llvm::Type *T =
2802 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2803 LongTy,
2804 llvm::ArrayType::get(ProtocolTyHolder, 0),
2805 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002806 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2807
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002808 // struct _objc_protocol {
2809 // struct _objc_protocol_extension *isa;
2810 // char *protocol_name;
2811 // struct _objc_protocol **_objc_protocol_list;
2812 // struct _objc_method_description_list *instance_methods;
2813 // struct _objc_method_description_list *class_methods;
2814 // }
2815 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002816 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002817 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2818 MethodDescriptionListPtrTy,
2819 MethodDescriptionListPtrTy,
2820 NULL);
2821 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2822
2823 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2824 CGM.getModule().addTypeName("struct._objc_protocol_list",
2825 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002826 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002827 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2828
2829 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002830 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002831 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002832
2833 // Class description structures
2834
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002835 // struct _objc_ivar {
2836 // char *ivar_name;
2837 // char *ivar_type;
2838 // int ivar_offset;
2839 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002840 IvarTy = llvm::StructType::get(Int8PtrTy,
2841 Int8PtrTy,
2842 IntTy,
2843 NULL);
2844 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2845
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002846 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002847 IvarListTy = llvm::OpaqueType::get();
2848 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2849 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2850
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002851 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002852 MethodListTy = llvm::OpaqueType::get();
2853 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2854 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2855
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002856 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002857 ClassExtensionTy =
2858 llvm::StructType::get(IntTy,
2859 Int8PtrTy,
2860 PropertyListPtrTy,
2861 NULL);
2862 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2863 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2864
2865 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2866
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002867 // struct _objc_class {
2868 // Class isa;
2869 // Class super_class;
2870 // char *name;
2871 // long version;
2872 // long info;
2873 // long instance_size;
2874 // struct _objc_ivar_list *ivars;
2875 // struct _objc_method_list *methods;
2876 // struct _objc_cache *cache;
2877 // struct _objc_protocol_list *protocols;
2878 // char *ivar_layout;
2879 // struct _objc_class_ext *ext;
2880 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002881 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2882 llvm::PointerType::getUnqual(ClassTyHolder),
2883 Int8PtrTy,
2884 LongTy,
2885 LongTy,
2886 LongTy,
2887 IvarListPtrTy,
2888 MethodListPtrTy,
2889 CachePtrTy,
2890 ProtocolListPtrTy,
2891 Int8PtrTy,
2892 ClassExtensionPtrTy,
2893 NULL);
2894 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2895
2896 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2897 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2898 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2899
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002900 // struct _objc_category {
2901 // char *category_name;
2902 // char *class_name;
2903 // struct _objc_method_list *instance_method;
2904 // struct _objc_method_list *class_method;
2905 // uint32_t size; // sizeof(struct _objc_category)
2906 // struct _objc_property_list *instance_properties;// category's @property
2907 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002908 CategoryTy = llvm::StructType::get(Int8PtrTy,
2909 Int8PtrTy,
2910 MethodListPtrTy,
2911 MethodListPtrTy,
2912 ProtocolListPtrTy,
2913 IntTy,
2914 PropertyListPtrTy,
2915 NULL);
2916 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2917
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002918 // Global metadata structures
2919
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002920 // struct _objc_symtab {
2921 // long sel_ref_cnt;
2922 // SEL *refs;
2923 // short cls_def_cnt;
2924 // short cat_def_cnt;
2925 // char *defs[cls_def_cnt + cat_def_cnt];
2926 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002927 SymtabTy = llvm::StructType::get(LongTy,
2928 SelectorPtrTy,
2929 ShortTy,
2930 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002931 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002932 NULL);
2933 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2934 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2935
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002936 // struct _objc_module {
2937 // long version;
2938 // long size; // sizeof(struct _objc_module)
2939 // char *name;
2940 // struct _objc_symtab* symtab;
2941 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002942 ModuleTy =
2943 llvm::StructType::get(LongTy,
2944 LongTy,
2945 Int8PtrTy,
2946 SymtabPtrTy,
2947 NULL);
2948 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002949
Daniel Dunbar49f66022008-09-24 03:38:44 +00002950 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002951
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002952 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002953 std::vector<const llvm::Type*> Params;
2954 Params.push_back(ObjectPtrTy);
2955 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002956 MessageSendFn =
2957 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2958 Params,
2959 true),
2960 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002961
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002962 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002963 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002964 Params.push_back(ObjectPtrTy);
2965 Params.push_back(SelectorPtrTy);
2966 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002967 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2968 Params,
2969 true),
2970 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002971
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002972 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002973 Params.clear();
2974 Params.push_back(ObjectPtrTy);
2975 Params.push_back(SelectorPtrTy);
2976 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002977 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002978 MessageSendFpretFn =
2979 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2980 Params,
2981 true),
2982 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002983
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002984 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002985 Params.clear();
2986 Params.push_back(SuperPtrTy);
2987 Params.push_back(SelectorPtrTy);
2988 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002989 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2990 Params,
2991 true),
2992 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002993
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002994 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2995 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002996 Params.clear();
2997 Params.push_back(Int8PtrTy);
2998 Params.push_back(SuperPtrTy);
2999 Params.push_back(SelectorPtrTy);
3000 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003001 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3002 Params,
3003 true),
3004 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003005
3006 // There is no objc_msgSendSuper_fpret? How can that work?
3007 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003008
Anders Carlsson124526b2008-09-09 10:10:21 +00003009 // FIXME: This is the size of the setjmp buffer and should be
3010 // target specific. 18 is what's used on 32-bit X86.
3011 uint64_t SetJmpBufferSize = 18;
3012
3013 // Exceptions
3014 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003015 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003016
3017 ExceptionDataTy =
3018 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3019 SetJmpBufferSize),
3020 StackPtrTy, NULL);
3021 CGM.getModule().addTypeName("struct._objc_exception_data",
3022 ExceptionDataTy);
3023
3024 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003025 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3026 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003027 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3028 Params,
3029 false),
3030 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003031 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003032 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3033 Params,
3034 false),
3035 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003036 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003037 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3038 Params,
3039 false),
3040 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003041
3042 Params.clear();
3043 Params.push_back(ClassPtrTy);
3044 Params.push_back(ObjectPtrTy);
3045 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003046 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3047 Params,
3048 false),
3049 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003050
3051 // synchronized APIs
3052 // void objc_sync_enter (id)
3053 Params.clear();
3054 Params.push_back(ObjectPtrTy);
3055 SyncEnterFn =
3056 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3057 Params,
3058 false),
3059 "objc_sync_enter");
3060 // void objc_sync_exit (id)
3061 SyncExitFn =
3062 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3063 Params,
3064 false),
3065 "objc_sync_exit");
3066
Anders Carlsson124526b2008-09-09 10:10:21 +00003067
3068 Params.clear();
3069 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3070 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003071 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3072 Params,
3073 false),
3074 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003075
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003076}
3077
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003078ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003079: ObjCCommonTypesHelper(cgm)
3080{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003081 // struct _method_list_t {
3082 // uint32_t entsize; // sizeof(struct _objc_method)
3083 // uint32_t method_count;
3084 // struct _objc_method method_list[method_count];
3085 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003086 MethodListnfABITy = llvm::StructType::get(IntTy,
3087 IntTy,
3088 llvm::ArrayType::get(MethodTy, 0),
3089 NULL);
3090 CGM.getModule().addTypeName("struct.__method_list_t",
3091 MethodListnfABITy);
3092 // struct method_list_t *
3093 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003094
3095 // struct _protocol_t {
3096 // id isa; // NULL
3097 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003098 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003099 // const struct method_list_t * const instance_methods;
3100 // const struct method_list_t * const class_methods;
3101 // const struct method_list_t *optionalInstanceMethods;
3102 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003103 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003104 // const uint32_t size; // sizeof(struct _protocol_t)
3105 // const uint32_t flags; // = 0
3106 // }
3107
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003108 // Holder for struct _protocol_list_t *
3109 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3110
3111 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3112 Int8PtrTy,
3113 llvm::PointerType::getUnqual(
3114 ProtocolListTyHolder),
3115 MethodListnfABIPtrTy,
3116 MethodListnfABIPtrTy,
3117 MethodListnfABIPtrTy,
3118 MethodListnfABIPtrTy,
3119 PropertyListPtrTy,
3120 IntTy,
3121 IntTy,
3122 NULL);
3123 CGM.getModule().addTypeName("struct._protocol_t",
3124 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003125
3126 // struct _protocol_t*
3127 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003128
Fariborz Jahanianda320092009-01-29 19:24:30 +00003129 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003130 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003131 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003132 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003133 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3134 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003135 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003136 NULL);
3137 CGM.getModule().addTypeName("struct._objc_protocol_list",
3138 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003139 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3140 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003141
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003142 // struct _objc_protocol_list*
3143 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003144
3145 // struct _ivar_t {
3146 // unsigned long int *offset; // pointer to ivar offset location
3147 // char *name;
3148 // char *type;
3149 // uint32_t alignment;
3150 // uint32_t size;
3151 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003152 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3153 Int8PtrTy,
3154 Int8PtrTy,
3155 IntTy,
3156 IntTy,
3157 NULL);
3158 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3159
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003160 // struct _ivar_list_t {
3161 // uint32 entsize; // sizeof(struct _ivar_t)
3162 // uint32 count;
3163 // struct _iver_t list[count];
3164 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003165 IvarListnfABITy = llvm::StructType::get(IntTy,
3166 IntTy,
3167 llvm::ArrayType::get(
3168 IvarnfABITy, 0),
3169 NULL);
3170 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3171
3172 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003173
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003174 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003175 // uint32_t const flags;
3176 // uint32_t const instanceStart;
3177 // uint32_t const instanceSize;
3178 // uint32_t const reserved; // only when building for 64bit targets
3179 // const uint8_t * const ivarLayout;
3180 // const char *const name;
3181 // const struct _method_list_t * const baseMethods;
3182 // const struct _objc_protocol_list *const baseProtocols;
3183 // const struct _ivar_list_t *const ivars;
3184 // const uint8_t * const weakIvarLayout;
3185 // const struct _prop_list_t * const properties;
3186 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003187
3188 // FIXME. Add 'reserved' field in 64bit abi mode!
3189 ClassRonfABITy = llvm::StructType::get(IntTy,
3190 IntTy,
3191 IntTy,
3192 Int8PtrTy,
3193 Int8PtrTy,
3194 MethodListnfABIPtrTy,
3195 ProtocolListnfABIPtrTy,
3196 IvarListnfABIPtrTy,
3197 Int8PtrTy,
3198 PropertyListPtrTy,
3199 NULL);
3200 CGM.getModule().addTypeName("struct._class_ro_t",
3201 ClassRonfABITy);
3202
3203 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3204 std::vector<const llvm::Type*> Params;
3205 Params.push_back(ObjectPtrTy);
3206 Params.push_back(SelectorPtrTy);
3207 ImpnfABITy = llvm::PointerType::getUnqual(
3208 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3209
3210 // struct _class_t {
3211 // struct _class_t *isa;
3212 // struct _class_t * const superclass;
3213 // void *cache;
3214 // IMP *vtable;
3215 // struct class_ro_t *ro;
3216 // }
3217
3218 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3219 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3220 llvm::PointerType::getUnqual(ClassTyHolder),
3221 CachePtrTy,
3222 llvm::PointerType::getUnqual(ImpnfABITy),
3223 llvm::PointerType::getUnqual(
3224 ClassRonfABITy),
3225 NULL);
3226 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3227
3228 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3229 ClassnfABITy);
3230
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003231 // LLVM for struct _class_t *
3232 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3233
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003234 // struct _category_t {
3235 // const char * const name;
3236 // struct _class_t *const cls;
3237 // const struct _method_list_t * const instance_methods;
3238 // const struct _method_list_t * const class_methods;
3239 // const struct _protocol_list_t * const protocols;
3240 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003241 // }
3242 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003243 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003244 MethodListnfABIPtrTy,
3245 MethodListnfABIPtrTy,
3246 ProtocolListnfABIPtrTy,
3247 PropertyListPtrTy,
3248 NULL);
3249 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003250
3251 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3253 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003254
3255 // MessageRefTy - LLVM for:
3256 // struct _message_ref_t {
3257 // IMP messenger;
3258 // SEL name;
3259 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003260
3261 // First the clang type for struct _message_ref_t
3262 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3263 SourceLocation(),
3264 &Ctx.Idents.get("_message_ref_t"));
3265 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3266 Ctx.VoidPtrTy, 0, false));
3267 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3268 Ctx.getObjCSelType(), 0, false));
3269 RD->completeDefinition(Ctx);
3270
3271 MessageRefCTy = Ctx.getTagDeclType(RD);
3272 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3273 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003274
3275 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3276 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3277
3278 // SuperMessageRefTy - LLVM for:
3279 // struct _super_message_ref_t {
3280 // SUPER_IMP messenger;
3281 // SEL name;
3282 // };
3283 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3284 SelectorPtrTy,
3285 NULL);
3286 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3287
3288 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3289 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3290
3291 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3292 Params.clear();
3293 Params.push_back(ObjectPtrTy);
3294 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003295 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3296 Params,
3297 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003298 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003299 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003300 "objc_msgSend_fixup");
3301
3302 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3303 MessageSendFpretFixupFn =
3304 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3305 Params,
3306 true),
3307 "objc_msgSend_fpret_fixup");
3308
3309 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3310 MessageSendStretFixupFn =
3311 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3312 Params,
3313 true),
3314 "objc_msgSend_stret_fixup");
3315
3316 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3317 MessageSendIdFixupFn =
3318 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3319 Params,
3320 true),
3321 "objc_msgSendId_fixup");
3322
3323
3324 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3325 MessageSendIdStretFixupFn =
3326 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3327 Params,
3328 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003329 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003330
3331 // id objc_msgSendSuper2_fixup (struct objc_super *,
3332 // struct _super_message_ref_t*, ...)
3333 Params.clear();
3334 Params.push_back(SuperPtrTy);
3335 Params.push_back(SuperMessageRefPtrTy);
3336 MessageSendSuper2FixupFn =
3337 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3338 Params,
3339 true),
3340 "objc_msgSendSuper2_fixup");
3341
3342
3343 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3344 // struct _super_message_ref_t*, ...)
3345 MessageSendSuper2StretFixupFn =
3346 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3347 Params,
3348 true),
3349 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003350
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003351}
3352
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003353llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3354 FinishNonFragileABIModule();
3355
3356 return NULL;
3357}
3358
3359void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3360 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003361
3362 // Build list of all implemented classe addresses in array
3363 // L_OBJC_LABEL_CLASS_$.
3364 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3365 // list of 'nonlazy' implementations (defined as those with a +load{}
3366 // method!!).
3367 unsigned NumClasses = DefinedClasses.size();
3368 if (NumClasses) {
3369 std::vector<llvm::Constant*> Symbols(NumClasses);
3370 for (unsigned i=0; i<NumClasses; i++)
3371 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3372 ObjCTypes.Int8PtrTy);
3373 llvm::Constant* Init =
3374 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3375 NumClasses),
3376 Symbols);
3377
3378 llvm::GlobalVariable *GV =
3379 new llvm::GlobalVariable(Init->getType(), false,
3380 llvm::GlobalValue::InternalLinkage,
3381 Init,
3382 "\01L_OBJC_LABEL_CLASS_$",
3383 &CGM.getModule());
3384 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3385 UsedGlobals.push_back(GV);
3386 }
3387
3388 // Build list of all implemented category addresses in array
3389 // L_OBJC_LABEL_CATEGORY_$.
3390 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3391 // list of 'nonlazy' category implementations (defined as those with a +load{}
3392 // method!!).
3393 unsigned NumCategory = DefinedCategories.size();
3394 if (NumCategory) {
3395 std::vector<llvm::Constant*> Symbols(NumCategory);
3396 for (unsigned i=0; i<NumCategory; i++)
3397 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3398 ObjCTypes.Int8PtrTy);
3399 llvm::Constant* Init =
3400 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3401 NumCategory),
3402 Symbols);
3403
3404 llvm::GlobalVariable *GV =
3405 new llvm::GlobalVariable(Init->getType(), false,
3406 llvm::GlobalValue::InternalLinkage,
3407 Init,
3408 "\01L_OBJC_LABEL_CATEGORY_$",
3409 &CGM.getModule());
3410 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3411 UsedGlobals.push_back(GV);
3412 }
3413
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003414 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3415 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3416 std::vector<llvm::Constant*> Values(2);
3417 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3418 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3419 llvm::Constant* Init = llvm::ConstantArray::get(
3420 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3421 Values);
3422 llvm::GlobalVariable *IMGV =
3423 new llvm::GlobalVariable(Init->getType(), false,
3424 llvm::GlobalValue::InternalLinkage,
3425 Init,
3426 "\01L_OBJC_IMAGE_INFO",
3427 &CGM.getModule());
3428 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3429 UsedGlobals.push_back(IMGV);
3430
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003431 std::vector<llvm::Constant*> Used;
3432 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3433 e = UsedGlobals.end(); i != e; ++i) {
3434 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3435 }
3436
3437 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3438 llvm::GlobalValue *GV =
3439 new llvm::GlobalVariable(AT, false,
3440 llvm::GlobalValue::AppendingLinkage,
3441 llvm::ConstantArray::get(AT, Used),
3442 "llvm.used",
3443 &CGM.getModule());
3444
3445 GV->setSection("llvm.metadata");
3446
3447}
3448
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003449// Metadata flags
3450enum MetaDataDlags {
3451 CLS = 0x0,
3452 CLS_META = 0x1,
3453 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003454 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003455 CLS_EXCEPTION = 0x20
3456};
3457/// BuildClassRoTInitializer - generate meta-data for:
3458/// struct _class_ro_t {
3459/// uint32_t const flags;
3460/// uint32_t const instanceStart;
3461/// uint32_t const instanceSize;
3462/// uint32_t const reserved; // only when building for 64bit targets
3463/// const uint8_t * const ivarLayout;
3464/// const char *const name;
3465/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003466/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003467/// const struct _ivar_list_t *const ivars;
3468/// const uint8_t * const weakIvarLayout;
3469/// const struct _prop_list_t * const properties;
3470/// }
3471///
3472llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3473 unsigned flags,
3474 unsigned InstanceStart,
3475 unsigned InstanceSize,
3476 const ObjCImplementationDecl *ID) {
3477 std::string ClassName = ID->getNameAsString();
3478 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3479 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3480 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3481 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3482 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003483 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003484 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3485 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003486 // const struct _method_list_t * const baseMethods;
3487 std::vector<llvm::Constant*> Methods;
3488 std::string MethodListName("\01l_OBJC_$_");
3489 if (flags & CLS_META) {
3490 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3491 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3492 e = ID->classmeth_end(); i != e; ++i) {
3493 // Class methods should always be defined.
3494 Methods.push_back(GetMethodConstant(*i));
3495 }
3496 } else {
3497 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3498 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3499 e = ID->instmeth_end(); i != e; ++i) {
3500 // Instance methods should always be defined.
3501 Methods.push_back(GetMethodConstant(*i));
3502 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003503 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3504 e = ID->propimpl_end(); i != e; ++i) {
3505 ObjCPropertyImplDecl *PID = *i;
3506
3507 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3508 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3509
3510 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3511 if (llvm::Constant *C = GetMethodConstant(MD))
3512 Methods.push_back(C);
3513 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3514 if (llvm::Constant *C = GetMethodConstant(MD))
3515 Methods.push_back(C);
3516 }
3517 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003518 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003519 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003520 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003521
3522 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3523 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3524 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3525 + OID->getNameAsString(),
3526 OID->protocol_begin(),
3527 OID->protocol_end());
3528
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003529 if (flags & CLS_META)
3530 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3531 else
3532 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003533 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003534 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003535 if (flags & CLS_META)
3536 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3537 else
3538 Values[ 9] =
3539 EmitPropertyList(
3540 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3541 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003542 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3543 Values);
3544 llvm::GlobalVariable *CLASS_RO_GV =
3545 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3546 llvm::GlobalValue::InternalLinkage,
3547 Init,
3548 (flags & CLS_META) ?
3549 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3550 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3551 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003552 CLASS_RO_GV->setAlignment(
3553 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003554 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003555 UsedGlobals.push_back(CLASS_RO_GV);
3556 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003557
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003558}
3559
3560/// BuildClassMetaData - This routine defines that to-level meta-data
3561/// for the given ClassName for:
3562/// struct _class_t {
3563/// struct _class_t *isa;
3564/// struct _class_t * const superclass;
3565/// void *cache;
3566/// IMP *vtable;
3567/// struct class_ro_t *ro;
3568/// }
3569///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003570llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3571 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003572 llvm::Constant *IsAGV,
3573 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003574 llvm::Constant *ClassRoGV,
3575 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003576 std::vector<llvm::Constant*> Values(5);
3577 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003578 Values[1] = SuperClassGV
3579 ? SuperClassGV
3580 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003581 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3582 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3583 Values[4] = ClassRoGV; // &CLASS_RO_GV
3584 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3585 Values);
3586 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3587 if (GV)
3588 GV->setInitializer(Init);
3589 else
3590 GV =
3591 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3592 llvm::GlobalValue::ExternalLinkage,
3593 Init,
3594 ClassName,
3595 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003596 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003597 GV->setAlignment(
3598 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003599 if (HiddenVisibility)
3600 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003601 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003602 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003603}
3604
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003605void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3606 std::string ClassName = ID->getNameAsString();
3607 if (!ObjCEmptyCacheVar) {
3608 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003609 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003610 false,
3611 llvm::GlobalValue::ExternalLinkage,
3612 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003613 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003614 &CGM.getModule());
3615 UsedGlobals.push_back(ObjCEmptyCacheVar);
3616
3617 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003618 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003619 false,
3620 llvm::GlobalValue::ExternalLinkage,
3621 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003622 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003623 &CGM.getModule());
3624 UsedGlobals.push_back(ObjCEmptyVtableVar);
3625 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003626 assert(ID->getClassInterface() &&
3627 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003628 uint32_t InstanceStart =
3629 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3630 uint32_t InstanceSize = InstanceStart;
3631 uint32_t flags = CLS_META;
3632 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3633 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003634
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003635 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003636
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003637 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3638 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003639 flags |= OBJC2_CLS_HIDDEN;
3640 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003641 // class is root
3642 flags |= CLS_ROOT;
3643 std::string SuperClassName = ObjCClassName + ClassName;
3644 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003645 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003646 SuperClassGV =
3647 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3648 llvm::GlobalValue::ExternalLinkage,
3649 0,
3650 SuperClassName,
3651 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003652 UsedGlobals.push_back(SuperClassGV);
3653 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003654 std::string IsAClassName = ObjCMetaClassName + ClassName;
3655 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003656 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003657 IsAGV =
3658 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3659 llvm::GlobalValue::ExternalLinkage,
3660 0,
3661 IsAClassName,
3662 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003663 UsedGlobals.push_back(IsAGV);
3664 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003665 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003666 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003667 std::string RootClassName =
3668 ID->getClassInterface()->getSuperClass()->getNameAsString();
3669 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3670 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003671 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003672 SuperClassGV =
3673 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3674 llvm::GlobalValue::ExternalLinkage,
3675 0,
3676 SuperClassName,
3677 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003678 UsedGlobals.push_back(SuperClassGV);
3679 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003680 IsAGV = SuperClassGV;
3681 }
3682 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3683 InstanceStart,
3684 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003685 std::string TClassName = ObjCMetaClassName + ClassName;
3686 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003687 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3688 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003689
3690 // Metadata for the class
3691 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003692 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003693 flags |= OBJC2_CLS_HIDDEN;
3694 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003695 flags |= CLS_ROOT;
3696 SuperClassGV = 0;
3697 }
3698 else {
3699 // Has a root. Current class is not a root.
3700 std::string RootClassName =
3701 ID->getClassInterface()->getSuperClass()->getNameAsString();
3702 std::string SuperClassName = ObjCClassName + RootClassName;
3703 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003704 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003705 SuperClassGV =
3706 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3707 llvm::GlobalValue::ExternalLinkage,
3708 0,
3709 SuperClassName,
3710 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003711 UsedGlobals.push_back(SuperClassGV);
3712 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003713 }
3714
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003715 InstanceStart = InstanceSize = 0;
3716 if (ObjCInterfaceDecl *OID =
3717 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3718 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003719 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003720 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003721 const llvm::StructLayout *Layout =
3722 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003723
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003724 RecordDecl::field_iterator firstField, lastField;
3725 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003726
3727 for (RecordDecl::field_iterator e = RD->field_end(),
3728 ifield = firstField; ifield != e; ++ifield)
3729 lastField = ifield;
3730
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003731 if (lastField != RD->field_end()) {
3732 FieldDecl *Field = *lastField;
3733 const llvm::Type *FieldTy =
3734 CGM.getTypes().ConvertTypeForMem(Field->getType());
3735 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3736 InstanceSize = Layout->getElementOffset(
3737 CGM.getTypes().getLLVMFieldNo(Field)) +
3738 Size;
3739 if (firstField == RD->field_end())
3740 InstanceStart = InstanceSize;
3741 else
3742 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3743 getLLVMFieldNo(*firstField));
3744 }
3745 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003746 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003747 InstanceStart,
3748 InstanceSize,
3749 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003750
3751 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003752 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003753 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3754 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003755 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003756}
3757
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003758/// GenerateProtocolRef - This routine is called to generate code for
3759/// a protocol reference expression; as in:
3760/// @code
3761/// @protocol(Proto1);
3762/// @endcode
3763/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3764/// which will hold address of the protocol meta-data.
3765///
3766llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3767 const ObjCProtocolDecl *PD) {
3768
3769 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3770 ObjCTypes.ExternalProtocolPtrTy);
3771
3772 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3773 ProtocolName += PD->getNameAsCString();
3774
3775 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3776 if (PTGV)
3777 return Builder.CreateLoad(PTGV, false, "tmp");
3778 PTGV = new llvm::GlobalVariable(
3779 Init->getType(), false,
3780 llvm::GlobalValue::WeakLinkage,
3781 Init,
3782 ProtocolName,
3783 &CGM.getModule());
3784 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3785 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3786 UsedGlobals.push_back(PTGV);
3787 return Builder.CreateLoad(PTGV, false, "tmp");
3788}
3789
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003790/// GenerateCategory - Build metadata for a category implementation.
3791/// struct _category_t {
3792/// const char * const name;
3793/// struct _class_t *const cls;
3794/// const struct _method_list_t * const instance_methods;
3795/// const struct _method_list_t * const class_methods;
3796/// const struct _protocol_list_t * const protocols;
3797/// const struct _prop_list_t * const properties;
3798/// }
3799///
3800void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3801{
3802 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003803 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3804 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003805 "_$_" + OCD->getNameAsString());
3806 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3807
3808 std::vector<llvm::Constant*> Values(6);
3809 Values[0] = GetClassName(OCD->getIdentifier());
3810 // meta-class entry symbol
3811 llvm::GlobalVariable *ClassGV =
3812 CGM.getModule().getGlobalVariable(ExtClassName);
3813 if (!ClassGV)
3814 ClassGV =
3815 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3816 llvm::GlobalValue::ExternalLinkage,
3817 0,
3818 ExtClassName,
3819 &CGM.getModule());
3820 UsedGlobals.push_back(ClassGV);
3821 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003822 std::vector<llvm::Constant*> Methods;
3823 std::string MethodListName(Prefix);
3824 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3825 "_$_" + OCD->getNameAsString();
3826
3827 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3828 e = OCD->instmeth_end(); i != e; ++i) {
3829 // Instance methods should always be defined.
3830 Methods.push_back(GetMethodConstant(*i));
3831 }
3832
3833 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003834 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003835 Methods);
3836
3837 MethodListName = Prefix;
3838 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3839 OCD->getNameAsString();
3840 Methods.clear();
3841 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3842 e = OCD->classmeth_end(); i != e; ++i) {
3843 // Class methods should always be defined.
3844 Methods.push_back(GetMethodConstant(*i));
3845 }
3846
3847 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003848 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003849 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003850 const ObjCCategoryDecl *Category =
3851 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003852 if (Category) {
3853 std::string ExtName(Interface->getNameAsString() + "_$_" +
3854 OCD->getNameAsString());
3855 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3856 + Interface->getNameAsString() + "_$_"
3857 + Category->getNameAsString(),
3858 Category->protocol_begin(),
3859 Category->protocol_end());
3860 Values[5] =
3861 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3862 OCD, Category, ObjCTypes);
3863 }
3864 else {
3865 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3866 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3867 }
3868
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003869 llvm::Constant *Init =
3870 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3871 Values);
3872 llvm::GlobalVariable *GCATV
3873 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3874 false,
3875 llvm::GlobalValue::InternalLinkage,
3876 Init,
3877 ExtCatName,
3878 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003879 GCATV->setAlignment(
3880 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003881 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003882 UsedGlobals.push_back(GCATV);
3883 DefinedCategories.push_back(GCATV);
3884}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003885
3886/// GetMethodConstant - Return a struct objc_method constant for the
3887/// given method if it has been defined. The result is null if the
3888/// method has not been defined. The return value has type MethodPtrTy.
3889llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3890 const ObjCMethodDecl *MD) {
3891 // FIXME: Use DenseMap::lookup
3892 llvm::Function *Fn = MethodDefinitions[MD];
3893 if (!Fn)
3894 return 0;
3895
3896 std::vector<llvm::Constant*> Method(3);
3897 Method[0] =
3898 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3899 ObjCTypes.SelectorPtrTy);
3900 Method[1] = GetMethodVarType(MD);
3901 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3902 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3903}
3904
3905/// EmitMethodList - Build meta-data for method declarations
3906/// struct _method_list_t {
3907/// uint32_t entsize; // sizeof(struct _objc_method)
3908/// uint32_t method_count;
3909/// struct _objc_method method_list[method_count];
3910/// }
3911///
3912llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3913 const std::string &Name,
3914 const char *Section,
3915 const ConstantVector &Methods) {
3916 // Return null for empty list.
3917 if (Methods.empty())
3918 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3919
3920 std::vector<llvm::Constant*> Values(3);
3921 // sizeof(struct _objc_method)
3922 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3923 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3924 // method_count
3925 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3926 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3927 Methods.size());
3928 Values[2] = llvm::ConstantArray::get(AT, Methods);
3929 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3930
3931 llvm::GlobalVariable *GV =
3932 new llvm::GlobalVariable(Init->getType(), false,
3933 llvm::GlobalValue::InternalLinkage,
3934 Init,
3935 Name,
3936 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003937 GV->setAlignment(
3938 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003939 GV->setSection(Section);
3940 UsedGlobals.push_back(GV);
3941 return llvm::ConstantExpr::getBitCast(GV,
3942 ObjCTypes.MethodListnfABIPtrTy);
3943}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003944
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003945/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3946/// the given ivar.
3947///
3948llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3949 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003950 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003951 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003952 Name += "\01_OBJC_IVAR_$_" +
3953 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003954 + Ivar->getNameAsString();
3955 llvm::GlobalVariable *IvarOffsetGV =
3956 CGM.getModule().getGlobalVariable(Name);
3957 if (!IvarOffsetGV)
3958 IvarOffsetGV =
3959 new llvm::GlobalVariable(ObjCTypes.LongTy,
3960 false,
3961 llvm::GlobalValue::ExternalLinkage,
3962 0,
3963 Name,
3964 &CGM.getModule());
3965 return IvarOffsetGV;
3966}
3967
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003968llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003969 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003970 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003971 unsigned long int Offset) {
3972
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003973 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003974 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003975 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003976 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003977
3978 llvm::GlobalVariable *IvarOffsetGV =
3979 CGM.getModule().getGlobalVariable(ExternalName);
3980 if (IvarOffsetGV) {
3981 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003982 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003983 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003984 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003985 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003986 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003987 return IvarOffsetGV;
3988 }
3989
3990 IvarOffsetGV =
3991 new llvm::GlobalVariable(Init->getType(),
3992 false,
3993 llvm::GlobalValue::ExternalLinkage,
3994 Init,
3995 ExternalName,
3996 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003997 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003998 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003999 // @private and @package have hidden visibility.
4000 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4001 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4002 if (!globalVisibility)
4003 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004004 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004005 if (IsClassHidden(ID))
4006 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004007
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004008 IvarOffsetGV->setSection("__DATA, __objc_const");
4009 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004010 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004011}
4012
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004013/// EmitIvarList - Emit the ivar list for the given
4014/// implementation. If ForClass is true the list of class ivars
4015/// (i.e. metaclass ivars) is emitted, otherwise the list of
4016/// interface ivars will be emitted. The return value has type
4017/// IvarListnfABIPtrTy.
4018/// struct _ivar_t {
4019/// unsigned long int *offset; // pointer to ivar offset location
4020/// char *name;
4021/// char *type;
4022/// uint32_t alignment;
4023/// uint32_t size;
4024/// }
4025/// struct _ivar_list_t {
4026/// uint32 entsize; // sizeof(struct _ivar_t)
4027/// uint32 count;
4028/// struct _iver_t list[count];
4029/// }
4030///
4031llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4032 const ObjCImplementationDecl *ID) {
4033
4034 std::vector<llvm::Constant*> Ivars, Ivar(5);
4035
4036 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4037 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4038
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004039 // FIXME. Consolidate this with similar code in GenerateClass.
4040 const llvm::Type *InterfaceTy =
4041 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4042 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004043 const llvm::StructLayout *Layout =
4044 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004045
4046 RecordDecl::field_iterator i,p;
4047 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004048 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4049
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004050 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004051 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004052 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4053 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004054 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004055 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004056 if (Field->getIdentifier())
4057 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4058 else
4059 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4060 std::string TypeStr;
4061 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4062 Ivar[2] = GetMethodVarType(TypeStr);
4063 const llvm::Type *FieldTy =
4064 CGM.getTypes().ConvertTypeForMem(Field->getType());
4065 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4066 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4067 Field->getType().getTypePtr()) >> 3;
4068 Align = llvm::Log2_32(Align);
4069 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004070 // NOTE. Size of a bitfield does not match gcc's, because of the way
4071 // bitfields are treated special in each. But I am told that 'size'
4072 // for bitfield ivars is ignored by the runtime so it does not matter.
4073 // (even if it matters, some day, there is enough info. to get the bitfield
4074 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004075 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4076 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4077 }
4078 // Return null for empty list.
4079 if (Ivars.empty())
4080 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4081 std::vector<llvm::Constant*> Values(3);
4082 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4083 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4084 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4085 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4086 Ivars.size());
4087 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4088 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4089 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4090 llvm::GlobalVariable *GV =
4091 new llvm::GlobalVariable(Init->getType(), false,
4092 llvm::GlobalValue::InternalLinkage,
4093 Init,
4094 Prefix + OID->getNameAsString(),
4095 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004096 GV->setAlignment(
4097 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004098 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004099
4100 UsedGlobals.push_back(GV);
4101 return llvm::ConstantExpr::getBitCast(GV,
4102 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004103}
4104
4105llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4106 const ObjCProtocolDecl *PD) {
4107 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4108
4109 if (!Entry) {
4110 // We use the initializer as a marker of whether this is a forward
4111 // reference or not. At module finalization we add the empty
4112 // contents for protocols which were referenced but never defined.
4113 Entry =
4114 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4115 llvm::GlobalValue::ExternalLinkage,
4116 0,
4117 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4118 &CGM.getModule());
4119 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4120 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004121 }
4122
4123 return Entry;
4124}
4125
4126/// GetOrEmitProtocol - Generate the protocol meta-data:
4127/// @code
4128/// struct _protocol_t {
4129/// id isa; // NULL
4130/// const char * const protocol_name;
4131/// const struct _protocol_list_t * protocol_list; // super protocols
4132/// const struct method_list_t * const instance_methods;
4133/// const struct method_list_t * const class_methods;
4134/// const struct method_list_t *optionalInstanceMethods;
4135/// const struct method_list_t *optionalClassMethods;
4136/// const struct _prop_list_t * properties;
4137/// const uint32_t size; // sizeof(struct _protocol_t)
4138/// const uint32_t flags; // = 0
4139/// }
4140/// @endcode
4141///
4142
4143llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4144 const ObjCProtocolDecl *PD) {
4145 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4146
4147 // Early exit if a defining object has already been generated.
4148 if (Entry && Entry->hasInitializer())
4149 return Entry;
4150
4151 const char *ProtocolName = PD->getNameAsCString();
4152
4153 // Construct method lists.
4154 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4155 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4156 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4157 e = PD->instmeth_end(); i != e; ++i) {
4158 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004159 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004160 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4161 OptInstanceMethods.push_back(C);
4162 } else {
4163 InstanceMethods.push_back(C);
4164 }
4165 }
4166
4167 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4168 e = PD->classmeth_end(); i != e; ++i) {
4169 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004170 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004171 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4172 OptClassMethods.push_back(C);
4173 } else {
4174 ClassMethods.push_back(C);
4175 }
4176 }
4177
4178 std::vector<llvm::Constant*> Values(10);
4179 // isa is NULL
4180 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4181 Values[1] = GetClassName(PD->getIdentifier());
4182 Values[2] = EmitProtocolList(
4183 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4184 PD->protocol_begin(),
4185 PD->protocol_end());
4186
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004187 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004188 + PD->getNameAsString(),
4189 "__DATA, __objc_const",
4190 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004191 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004192 + PD->getNameAsString(),
4193 "__DATA, __objc_const",
4194 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004195 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004196 + PD->getNameAsString(),
4197 "__DATA, __objc_const",
4198 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004199 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004200 + PD->getNameAsString(),
4201 "__DATA, __objc_const",
4202 OptClassMethods);
4203 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4204 0, PD, ObjCTypes);
4205 uint32_t Size =
4206 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4207 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4208 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4209 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4210 Values);
4211
4212 if (Entry) {
4213 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004214 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004215 Entry->setInitializer(Init);
4216 } else {
4217 Entry =
4218 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004219 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004220 Init,
4221 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4222 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004223 Entry->setAlignment(
4224 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004225 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004226 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004227 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4228
4229 // Use this protocol meta-data to build protocol list table in section
4230 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004231 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004232 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004233 llvm::GlobalValue::WeakLinkage,
4234 Entry,
4235 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4236 +ProtocolName,
4237 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004238 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004239 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004240 PTGV->setSection("__DATA, __objc_protolist");
4241 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4242 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004243 return Entry;
4244}
4245
4246/// EmitProtocolList - Generate protocol list meta-data:
4247/// @code
4248/// struct _protocol_list_t {
4249/// long protocol_count; // Note, this is 32/64 bit
4250/// struct _protocol_t[protocol_count];
4251/// }
4252/// @endcode
4253///
4254llvm::Constant *
4255CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4256 ObjCProtocolDecl::protocol_iterator begin,
4257 ObjCProtocolDecl::protocol_iterator end) {
4258 std::vector<llvm::Constant*> ProtocolRefs;
4259
Fariborz Jahanianda320092009-01-29 19:24:30 +00004260 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004261 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004262 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4263
Daniel Dunbar948e2582009-02-15 07:36:20 +00004264 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004265 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4266 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004267 return llvm::ConstantExpr::getBitCast(GV,
4268 ObjCTypes.ProtocolListnfABIPtrTy);
4269
4270 for (; begin != end; ++begin)
4271 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4272
Fariborz Jahanianda320092009-01-29 19:24:30 +00004273 // This list is null terminated.
4274 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004275 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004276
4277 std::vector<llvm::Constant*> Values(2);
4278 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4279 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004280 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004281 ProtocolRefs.size()),
4282 ProtocolRefs);
4283
4284 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4285 GV = new llvm::GlobalVariable(Init->getType(), false,
4286 llvm::GlobalValue::InternalLinkage,
4287 Init,
4288 Name,
4289 &CGM.getModule());
4290 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004291 GV->setAlignment(
4292 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004293 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004294 return llvm::ConstantExpr::getBitCast(GV,
4295 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004296}
4297
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004298/// GetMethodDescriptionConstant - This routine build following meta-data:
4299/// struct _objc_method {
4300/// SEL _cmd;
4301/// char *method_type;
4302/// char *_imp;
4303/// }
4304
4305llvm::Constant *
4306CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4307 std::vector<llvm::Constant*> Desc(3);
4308 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4309 ObjCTypes.SelectorPtrTy);
4310 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004311 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004312 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4313 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4314}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004315
4316/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4317/// This code gen. amounts to generating code for:
4318/// @code
4319/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4320/// @encode
4321///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004322LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004323 CodeGen::CodeGenFunction &CGF,
4324 QualType ObjectTy,
4325 llvm::Value *BaseValue,
4326 const ObjCIvarDecl *Ivar,
4327 const FieldDecl *Field,
4328 unsigned CVRQualifiers) {
4329 assert(ObjectTy->isObjCInterfaceType() &&
4330 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004331 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004332 std::string ExternalName;
4333 llvm::GlobalVariable *IvarOffsetGV =
4334 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004335
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004336 // (char *) BaseValue
4337 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4338 ObjCTypes.Int8PtrTy);
4339 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4340 // (char*)BaseValue + Offset_symbol
4341 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4342 // (type *)((char*)BaseValue + Offset_symbol)
4343 const llvm::Type *IvarTy =
4344 CGM.getTypes().ConvertType(Ivar->getType());
4345 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4346 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004347
4348 if (Ivar->isBitField())
4349 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4350 CVRQualifiers);
4351
4352 LValue LV = LValue::MakeAddr(V,
4353 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4354 LValue::SetObjCIvar(LV, true);
4355 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004356}
4357
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004358llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4359 CodeGen::CodeGenFunction &CGF,
4360 ObjCInterfaceDecl *Interface,
4361 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004362 std::string ExternalName;
4363 llvm::GlobalVariable *IvarOffsetGV =
4364 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4365
4366 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004367}
4368
Fariborz Jahanian46551122009-02-04 00:22:57 +00004369CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4370 CodeGen::CodeGenFunction &CGF,
4371 QualType ResultType,
4372 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004373 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004374 QualType Arg0Ty,
4375 bool IsSuper,
4376 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004377 // FIXME. Even though IsSuper is passes. This function doese not
4378 // handle calls to 'super' receivers.
4379 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004380 llvm::Value *Arg0 = Receiver;
4381 if (!IsSuper)
4382 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004383
4384 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004385 // FIXME. This is too much work to get the ABI-specific result type
4386 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004387 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4388 llvm::SmallVector<QualType, 16>());
4389 llvm::Constant *Fn;
4390 std::string Name("\01l_");
4391 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004392#if 0
4393 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004394 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4395 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4396 // FIXME. Is there a better way of getting these names.
4397 // They are available in RuntimeFunctions vector pair.
4398 Name += "objc_msgSendId_stret_fixup";
4399 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004400 else
4401#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004402 if (IsSuper) {
4403 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4404 Name += "objc_msgSendSuper2_stret_fixup";
4405 }
4406 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004407 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004408 Fn = ObjCTypes.MessageSendStretFixupFn;
4409 Name += "objc_msgSend_stret_fixup";
4410 }
4411 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004412 else if (ResultType->isFloatingType() &&
4413 // Selection of frret API only happens in 32bit nonfragile ABI.
4414 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004415 Fn = ObjCTypes.MessageSendFpretFixupFn;
4416 Name += "objc_msgSend_fpret_fixup";
4417 }
4418 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004419#if 0
4420// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004421 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4422 Fn = ObjCTypes.MessageSendIdFixupFn;
4423 Name += "objc_msgSendId_fixup";
4424 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004425 else
4426#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004427 if (IsSuper) {
4428 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4429 Name += "objc_msgSendSuper2_fixup";
4430 }
4431 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004432 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004433 Fn = ObjCTypes.MessageSendFixupFn;
4434 Name += "objc_msgSend_fixup";
4435 }
4436 }
4437 Name += '_';
4438 std::string SelName(Sel.getAsString());
4439 // Replace all ':' in selector name with '_' ouch!
4440 for(unsigned i = 0; i < SelName.size(); i++)
4441 if (SelName[i] == ':')
4442 SelName[i] = '_';
4443 Name += SelName;
4444 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4445 if (!GV) {
4446 // Build messafe ref table entry.
4447 std::vector<llvm::Constant*> Values(2);
4448 Values[0] = Fn;
4449 Values[1] = GetMethodVarName(Sel);
4450 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4451 GV = new llvm::GlobalVariable(Init->getType(), false,
4452 llvm::GlobalValue::WeakLinkage,
4453 Init,
4454 Name,
4455 &CGM.getModule());
4456 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4457 GV->setAlignment(
4458 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4459 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4460 UsedGlobals.push_back(GV);
4461 }
4462 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004463
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004464 CallArgList ActualArgs;
4465 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4466 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4467 ObjCTypes.MessageRefCPtrTy));
4468 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004469 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4470 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4471 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004472 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004473 Callee = CGF.Builder.CreateBitCast(Callee,
4474 llvm::PointerType::getUnqual(FTy));
4475 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004476}
4477
4478/// Generate code for a message send expression in the nonfragile abi.
4479CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4480 CodeGen::CodeGenFunction &CGF,
4481 QualType ResultType,
4482 Selector Sel,
4483 llvm::Value *Receiver,
4484 bool IsClassMessage,
4485 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004486 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004487 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004488 false, CallArgs);
4489}
4490
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004491llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004492 const ObjCInterfaceDecl *ID,
4493 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004494
4495 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4496
4497 if (!Entry) {
4498 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4499 llvm::GlobalVariable *ClassGV =
4500 CGM.getModule().getGlobalVariable(ClassName);
4501 if (!ClassGV) {
4502 ClassGV =
4503 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4504 llvm::GlobalValue::ExternalLinkage,
4505 0,
4506 ClassName,
4507 &CGM.getModule());
4508 UsedGlobals.push_back(ClassGV);
4509 }
4510 Entry =
4511 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4512 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004513 ClassGV,
4514 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4515 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004516 &CGM.getModule());
4517 Entry->setAlignment(
4518 CGM.getTargetData().getPrefTypeAlignment(
4519 ObjCTypes.ClassnfABIPtrTy));
4520
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004521 if (IsSuper)
4522 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4523 else
4524 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004525 UsedGlobals.push_back(Entry);
4526 }
4527
4528 return Builder.CreateLoad(Entry, false, "tmp");
4529}
4530
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004531/// EmitMetaClassRef - Return a Value * of the address of _class_t
4532/// meta-data
4533///
4534llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4535 const ObjCInterfaceDecl *ID) {
4536 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4537 if (Entry)
4538 return Builder.CreateLoad(Entry, false, "tmp");
4539
4540 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4541 llvm::GlobalVariable *MetaClassGV =
4542 CGM.getModule().getGlobalVariable(MetaClassName);
4543 if (!MetaClassGV) {
4544 MetaClassGV =
4545 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4546 llvm::GlobalValue::ExternalLinkage,
4547 0,
4548 MetaClassName,
4549 &CGM.getModule());
4550 UsedGlobals.push_back(MetaClassGV);
4551 }
4552
4553 Entry =
4554 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4555 llvm::GlobalValue::InternalLinkage,
4556 MetaClassGV,
4557 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4558 &CGM.getModule());
4559 Entry->setAlignment(
4560 CGM.getTargetData().getPrefTypeAlignment(
4561 ObjCTypes.ClassnfABIPtrTy));
4562
4563 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4564 UsedGlobals.push_back(Entry);
4565
4566 return Builder.CreateLoad(Entry, false, "tmp");
4567}
4568
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004569/// GetClass - Return a reference to the class for the given interface
4570/// decl.
4571llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4572 const ObjCInterfaceDecl *ID) {
4573 return EmitClassRef(Builder, ID);
4574}
4575
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004576/// Generates a message send where the super is the receiver. This is
4577/// a message send to self with special delivery semantics indicating
4578/// which class's method should be called.
4579CodeGen::RValue
4580CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4581 QualType ResultType,
4582 Selector Sel,
4583 const ObjCInterfaceDecl *Class,
4584 llvm::Value *Receiver,
4585 bool IsClassMessage,
4586 const CodeGen::CallArgList &CallArgs) {
4587 // ...
4588 // Create and init a super structure; this is a (receiver, class)
4589 // pair we will pass to objc_msgSendSuper.
4590 llvm::Value *ObjCSuper =
4591 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4592
4593 llvm::Value *ReceiverAsObject =
4594 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4595 CGF.Builder.CreateStore(ReceiverAsObject,
4596 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4597
4598 // If this is a class message the metaclass is passed as the target.
4599 llvm::Value *Target =
4600 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4601 : EmitClassRef(CGF.Builder, Class, true);
4602
4603 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4604 // and ObjCTypes types.
4605 const llvm::Type *ClassTy =
4606 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4607 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4608 CGF.Builder.CreateStore(Target,
4609 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4610
4611 return EmitMessageSend(CGF, ResultType, Sel,
4612 ObjCSuper, ObjCTypes.SuperPtrCTy,
4613 true, CallArgs);
4614}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004615
4616llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4617 Selector Sel) {
4618 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4619
4620 if (!Entry) {
4621 llvm::Constant *Casted =
4622 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4623 ObjCTypes.SelectorPtrTy);
4624 Entry =
4625 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4626 llvm::GlobalValue::InternalLinkage,
4627 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4628 &CGM.getModule());
4629 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4630 UsedGlobals.push_back(Entry);
4631 }
4632
4633 return Builder.CreateLoad(Entry, false, "tmp");
4634}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004635/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4636/// objc_assign_ivar (id src, id *dst)
4637///
4638void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4639 llvm::Value *src, llvm::Value *dst)
4640{
4641 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4642 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4643 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4644 src, dst, "assignivar");
4645 return;
4646}
4647
4648/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4649/// objc_assign_strongCast (id src, id *dst)
4650///
4651void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4652 CodeGen::CodeGenFunction &CGF,
4653 llvm::Value *src, llvm::Value *dst)
4654{
4655 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4656 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4657 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4658 src, dst, "weakassign");
4659 return;
4660}
4661
4662/// EmitObjCWeakRead - Code gen for loading value of a __weak
4663/// object: objc_read_weak (id *src)
4664///
4665llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4666 CodeGen::CodeGenFunction &CGF,
4667 llvm::Value *AddrWeakObj)
4668{
4669 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4670 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4671 AddrWeakObj, "weakread");
4672 return read_weak;
4673}
4674
4675/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4676/// objc_assign_weak (id src, id *dst)
4677///
4678void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4679 llvm::Value *src, llvm::Value *dst)
4680{
4681 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4682 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4683 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4684 src, dst, "weakassign");
4685 return;
4686}
4687
4688/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4689/// objc_assign_global (id src, id *dst)
4690///
4691void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4692 llvm::Value *src, llvm::Value *dst)
4693{
4694 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4695 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4696 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4697 src, dst, "globalassign");
4698 return;
4699}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004700
Anders Carlssonf57c5b22009-02-16 22:59:18 +00004701/// EmitThrowStmt - Generate code for a throw statement.
4702void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
4703 const ObjCAtThrowStmt &S) {
4704 llvm::Value *ExceptionAsObject;
4705
4706 if (const Expr *ThrowExpr = S.getThrowExpr()) {
4707 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
4708 ExceptionAsObject =
4709 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
4710
4711 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
4712 CGF.Builder.CreateUnreachable();
4713 } else {
4714 CGF.ErrorUnsupported(&S, "rethrow statement");
4715 }
4716
4717 // Clear the insertion point to indicate we are in unreachable code.
4718 CGF.Builder.ClearInsertionPoint();
4719}
4720
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004721/* *** */
4722
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004723CodeGen::CGObjCRuntime *
4724CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004725 return new CGObjCMac(CGM);
4726}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004727
4728CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004729CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004730 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004731}