blob: 456530dfe0a52d354ba13e79722c1c9328cc095f [file] [log] [blame]
Daniel Dunbar8c85fac2008-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 Dunbar1be1df32008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar35b777f2008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000032
Daniel Dunbarfe131f02008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000041
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000042public:
Daniel Dunbarb050fa62008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000045
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000057
Daniel Dunbar0ed60b02008-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 Jahanian48543f52009-01-21 22:04:16 +000062
Daniel Dunbar15245e52008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000067
Fariborz Jahaniand0374812009-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 Jahanian781f2732009-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 Jahanian4b161702009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbar15245e52008-08-23 04:28:29 +0000108
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000125
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattnerdd978702008-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 Dunbardaf4ad42008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbaraecef4c2008-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 Dunbardaf4ad42008-08-12 00:12:39 +0000213};
214
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000218public:
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000219 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
220 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
221 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
222 *MessageSendSuper2StretFixupFn;
223
Fariborz Jahanian781f2732009-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 Dunbar1f42bb02009-02-15 07:36:20 +0000233 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
234 const llvm::Type *ProtocolnfABIPtrTy;
235
Fariborz Jahanian781f2732009-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 Jahanianc2a1c3e2009-01-23 23:53:38 +0000245 // ClassnfABIPtrTy - LLVM for struct _class_t*
246 const llvm::Type *ClassnfABIPtrTy;
247
Fariborz Jahanian781f2732009-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 Jahanian711e8dd2009-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 Jahanianf52110f2009-02-04 20:42:28 +0000274 // MessageRefCTy - clang type for struct _message_ref_t
275 QualType MessageRefCTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000276
277 // MessageRefPtrTy - LLVM for struct _message_ref_t*
278 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000279 // MessageRefCPtrTy - clang type for struct _message_ref_t*
280 QualType MessageRefCPtrTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000281
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +0000282 // MessengerTy - Type of the messenger (shown as IMP above)
283 const llvm::FunctionType *MessengerTy;
284
Fariborz Jahanian711e8dd2009-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 Jahaniand0374812009-01-22 23:02:58 +0000295 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
296 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian48543f52009-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 Dunbardaf4ad42008-08-12 00:12:39 +0000303 unsigned ObjCABI;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000304
Daniel Dunbar8ede0052008-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 Jahanian48543f52009-01-21 22:04:16 +0000314
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000315 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000316 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000317
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000318 /// MethodVarNames - uniqued method variable names.
319 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000320
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +0000324
Daniel Dunbar12996f52008-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 Jahanian48543f52009-01-21 22:04:16 +0000328
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000329 /// PropertyNames - uniqued method variable names.
330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000331
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000332 /// ClassReferences - uniqued class references.
333 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000334
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000335 /// SelectorReferences - uniqued selector references.
336 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000337
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +0000342
Daniel Dunbar35b777f2008-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 Jahanian48543f52009-01-21 22:04:16 +0000346
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000347 /// DefinedClasses - List of defined classes.
348 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000349
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000350 /// DefinedCategories - List of defined categories.
351 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000352
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000353 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000354 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000355 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000356
Fariborz Jahanian32b5ea22009-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 Jahanian4c1e4612009-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 Jahanianf2a94cd2009-01-28 19:12:34 +0000388 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
389 RecordDecl::field_iterator &FIV,
390 RecordDecl::field_iterator &PIV);
Fariborz Jahanian7b709bb2009-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 Jahanian5fedf4f2009-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 Jahanian48543f52009-01-21 22:04:16 +0000403public:
404 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
405 { }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000406
407 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000408
409 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
410 const ObjCContainerDecl *CD=0);
Fariborz Jahanian5fedf4f2009-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 Jahanian48543f52009-01-21 22:04:16 +0000424};
425
426class CGObjCMac : public CGObjCCommonMac {
427private:
428 ObjCTypesHelper ObjCTypes;
Daniel Dunbar1be1df32008-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 Dunbarb8fe21b2008-08-12 06:48:42 +0000433 /// EmitModuleInfo - Another marker encoding module level
434 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000435 void EmitModuleInfo();
436
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000437 /// EmitModuleSymols - Emit module symbols, the list of defined
438 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000439 llvm::Constant *EmitModuleSymbols();
440
Daniel Dunbar1be1df32008-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 Dunbarcffcdac2008-08-13 03:21:16 +0000444
Daniel Dunbarb050fa62008-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 Dunbard916e6e2008-11-01 01:53:16 +0000452 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000453 const ObjCInterfaceDecl *ID);
454
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000455 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000456 QualType ResultType,
457 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000458 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000459 QualType Arg0Ty,
460 bool IsSuper,
461 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000462
Daniel Dunbarb050fa62008-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 Jahanianf2a94cd2009-01-28 19:12:34 +0000469 bool ForClass);
470
Daniel Dunbarb1ee5d62008-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 Dunbarb050fa62008-08-21 04:36:09 +0000476 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000477 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000478 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
479 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +0000480 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000481 const ConstantVector &Methods);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000482
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000483 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000484
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000485 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000486
487 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000488 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000489 llvm::Constant *EmitMethodList(const std::string &Name,
490 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000491 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000492
493 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-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 Dunbarfe131f02008-08-27 02:31:56 +0000504 llvm::Constant *EmitMethodDescList(const std::string &Name,
505 const char *Section,
506 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000507
Daniel Dunbar35b777f2008-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 Jahanian5fedf4f2009-01-29 19:24:30 +0000511 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-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 Jahanian5fedf4f2009-01-29 19:24:30 +0000517 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000518
Daniel Dunbarcffcdac2008-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 Dunbarfe131f02008-08-27 02:31:56 +0000523 llvm::Constant *
524 EmitProtocolExtension(const ObjCProtocolDecl *PD,
525 const ConstantVector &OptInstanceMethods,
526 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000527
528 /// EmitProtocolList - Generate the list of referenced
529 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000530 llvm::Constant *EmitProtocolList(const std::string &Name,
531 ObjCProtocolDecl::protocol_iterator begin,
532 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000533
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000534 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
535 /// for the given selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000536 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000537
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000538 public:
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000539 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000540
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000541 virtual llvm::Function *ModuleInitFunction();
542
Daniel Dunbara04840b2008-08-23 03:46:30 +0000543 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000544 QualType ResultType,
545 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000546 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000547 bool IsClassMessage,
548 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000549
Daniel Dunbara04840b2008-08-23 03:46:30 +0000550 virtual CodeGen::RValue
551 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000552 QualType ResultType,
553 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000554 const ObjCInterfaceDecl *Class,
555 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000556 bool IsClassMessage,
557 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +0000558
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000559 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000560 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000561
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000562 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000563
Daniel Dunbarac93e472008-08-15 22:20:32 +0000564 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000565
Daniel Dunbarac93e472008-08-15 22:20:32 +0000566 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000567
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000568 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000569 const ObjCProtocolDecl *PD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000570
Daniel Dunbarf7103722008-09-24 03:38:44 +0000571 virtual llvm::Function *GetPropertyGetFunction();
572 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000573 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000574
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000575 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
576 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000577 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
578 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000579 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000580 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000581 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
582 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000583 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
584 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000585 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
586 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000587 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
588 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +0000589
Fariborz Jahanianc912eb72009-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 Jahanian27cc6662009-02-10 19:02:04 +0000596 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
597 ObjCInterfaceDecl *Interface,
598 const ObjCIvarDecl *Ivar);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000599};
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000600
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000601class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000602private:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000603 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000604 llvm::GlobalVariable* ObjCEmptyCacheVar;
605 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanianf3a44012009-02-06 20:09:23 +0000606 /// MetaClassReferences - uniqued meta class references.
607 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianc2a1c3e2009-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 Jahanian4c1e4612009-01-24 20:21:50 +0000613 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
614 unsigned InstanceStart,
615 unsigned InstanceSize,
616 const ObjCImplementationDecl *ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +0000617 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
618 llvm::Constant *IsAGV,
619 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +0000620 llvm::Constant *ClassRoGV,
621 bool HiddenVisibility);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000622
623 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
624
Fariborz Jahanian151747b2009-01-30 00:46:37 +0000625 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
626
Fariborz Jahaniana9502ca2009-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 Jahanian3f1cc562009-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 Jahanian4c1e4612009-01-24 20:21:50 +0000638
Fariborz Jahaniancc00f922009-02-10 20:21:06 +0000639 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +0000640 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +0000641 unsigned long int offset);
642
Fariborz Jahanian5fedf4f2009-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 Jahanian7e881162009-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 Jahanianf52110f2009-02-04 20:42:28 +0000663 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +0000664 QualType Arg0Ty,
665 bool IsSuper,
666 const CallArgList &CallArgs);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000667
Fariborz Jahanian917c0402009-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 Jahanianf3a44012009-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 Jahaniancc00f922009-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 Jahaniana09a5142009-02-12 18:51:23 +0000683 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +0000684 const ObjCIvarDecl *Ivar);
Fariborz Jahanian917c0402009-02-05 20:41:40 +0000685
Fariborz Jahanianebb82c62009-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 Jahanian48543f52009-01-21 22:04:16 +0000690public:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000691 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianc2a1c3e2009-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 Jahanian7e881162009-02-04 00:22:57 +0000700 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-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 Jahanianf3a44012009-02-06 20:09:23 +0000709 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000710
711 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian917c0402009-02-05 20:41:40 +0000712 const ObjCInterfaceDecl *ID);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000713
714 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanianebb82c62009-02-11 20:51:17 +0000715 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000716
Fariborz Jahanianfe49a092009-01-26 18:32:24 +0000717 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000718
719 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000720 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +0000721 const ObjCProtocolDecl *PD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000722
Fariborz Jahanian27cc6662009-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 Dunbar978d2be2009-02-16 18:48:45 +0000729 virtual llvm::Function *EnumerationMutationFunction() {
730 return ObjCTypes.EnumerationMutationFn;
731 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000732
733 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar978d2be2009-02-16 18:48:45 +0000734 const Stmt &S) {
735 CGF.ErrorUnsupported(&S, "try or synchronized statement");
736 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000737 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar978d2be2009-02-16 18:48:45 +0000738 const ObjCAtThrowStmt &S) {
739 CGF.ErrorUnsupported(&S, "throw statement");
740 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000741 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000742 llvm::Value *AddrWeakObj);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000743 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000744 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000745 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000746 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000747 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000748 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000749 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000750 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000751 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
752 QualType ObjectTy,
753 llvm::Value *BaseValue,
754 const ObjCIvarDecl *Ivar,
755 const FieldDecl *Field,
756 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000757 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
758 ObjCInterfaceDecl *Interface,
759 const ObjCIvarDecl *Ivar);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000760};
761
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000762} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000763
764/* *** Helper Functions *** */
765
766/// getConstantGEP() - Help routine to construct simple GEPs.
767static llvm::Constant *getConstantGEP(llvm::Constant *C,
768 unsigned idx0,
769 unsigned idx1) {
770 llvm::Value *Idxs[] = {
771 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
772 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
773 };
774 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
775}
776
777/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000778
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000779CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
780 ObjCTypes(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000781{
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000782 ObjCABI = 1;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000783 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000784}
785
Daniel Dunbar434627a2008-08-16 00:25:02 +0000786/// GetClass - Return a reference to the class for the given interface
787/// decl.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000788llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000789 const ObjCInterfaceDecl *ID) {
790 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000791}
792
793/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000794llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000795 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000796}
797
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000798/// Generate a constant CFString object.
799/*
800 struct __builtin_CFString {
801 const int *isa; // point to __CFConstantStringClassReference
802 int flags;
803 const char *str;
804 long length;
805 };
806*/
807
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000808llvm::Constant *CGObjCCommonMac::GenerateConstantString(
809 const std::string &String) {
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000810 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000811}
812
813/// Generates a message send where the super is the receiver. This is
814/// a message send to self with special delivery semantics indicating
815/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000816CodeGen::RValue
817CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000818 QualType ResultType,
819 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000820 const ObjCInterfaceDecl *Class,
821 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000822 bool IsClassMessage,
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000823 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +0000824 // Create and init a super structure; this is a (receiver, class)
825 // pair we will pass to objc_msgSendSuper.
826 llvm::Value *ObjCSuper =
827 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
828 llvm::Value *ReceiverAsObject =
829 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
830 CGF.Builder.CreateStore(ReceiverAsObject,
831 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +0000832
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000833 // If this is a class message the metaclass is passed as the target.
834 llvm::Value *Target;
835 if (IsClassMessage) {
836 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
837 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
838 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
839 Target = Super;
840 } else {
841 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
842 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000843 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
844 // and ObjCTypes types.
845 const llvm::Type *ClassTy =
846 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000847 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000848 CGF.Builder.CreateStore(Target,
849 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
850
Daniel Dunbardd851282008-08-30 05:35:15 +0000851 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000852 ObjCSuper, ObjCTypes.SuperPtrCTy,
853 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000854}
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000855
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000856/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000857CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000858 QualType ResultType,
859 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000860 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000861 bool IsClassMessage,
862 const CallArgList &CallArgs) {
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000863 llvm::Value *Arg0 =
864 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbardd851282008-08-30 05:35:15 +0000865 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000866 Arg0, CGF.getContext().getObjCIdType(),
867 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000868}
869
870CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000871 QualType ResultType,
872 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000873 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000874 QualType Arg0Ty,
875 bool IsSuper,
876 const CallArgList &CallArgs) {
877 CallArgList ActualArgs;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000878 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
879 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
880 Sel)),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000881 CGF.getContext().getObjCSelType()));
882 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000883
Daniel Dunbar34bda882009-02-02 23:23:47 +0000884 CodeGenTypes &Types = CGM.getTypes();
885 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
886 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000887
888 llvm::Constant *Fn;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000889 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000890 Fn = ObjCTypes.getSendStretFn(IsSuper);
891 } else if (ResultType->isFloatingType()) {
892 // FIXME: Sadly, this is wrong. This actually depends on the
893 // architecture. This happens to be right for x86-32 though.
894 Fn = ObjCTypes.getSendFpretFn(IsSuper);
895 } else {
896 Fn = ObjCTypes.getSendFn(IsSuper);
897 }
Daniel Dunbara9976a22008-09-10 07:00:50 +0000898 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000899 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000900}
901
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000902llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000903 const ObjCProtocolDecl *PD) {
Daniel Dunbarb3518152008-09-04 04:33:15 +0000904 // FIXME: I don't understand why gcc generates this, or where it is
905 // resolved. Investigate. Its also wasteful to look this up over and
906 // over.
907 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
908
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000909 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
910 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000911}
912
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000913void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000914 // FIXME: We shouldn't need this, the protocol decl should contain
915 // enough information to tell us whether this was a declaration or a
916 // definition.
917 DefinedProtocols.insert(PD->getIdentifier());
918
919 // If we have generated a forward reference to this protocol, emit
920 // it now. Otherwise do nothing, the protocol objects are lazily
921 // emitted.
922 if (Protocols.count(PD->getIdentifier()))
923 GetOrEmitProtocol(PD);
924}
925
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000926llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000927 if (DefinedProtocols.count(PD->getIdentifier()))
928 return GetOrEmitProtocol(PD);
929 return GetOrEmitProtocolRef(PD);
930}
931
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000932/*
933 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
934 struct _objc_protocol {
935 struct _objc_protocol_extension *isa;
936 char *protocol_name;
937 struct _objc_protocol_list *protocol_list;
938 struct _objc__method_prototype_list *instance_methods;
939 struct _objc__method_prototype_list *class_methods
940 };
941
942 See EmitProtocolExtension().
943*/
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000944llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
945 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
946
947 // Early exit if a defining object has already been generated.
948 if (Entry && Entry->hasInitializer())
949 return Entry;
950
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000951 // FIXME: I don't understand why gcc generates this, or where it is
952 // resolved. Investigate. Its also wasteful to look this up over and
953 // over.
954 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
955
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000956 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000957
958 // Construct method lists.
959 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
960 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
961 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
962 e = PD->instmeth_end(); i != e; ++i) {
963 ObjCMethodDecl *MD = *i;
964 llvm::Constant *C = GetMethodDescriptionConstant(MD);
965 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
966 OptInstanceMethods.push_back(C);
967 } else {
968 InstanceMethods.push_back(C);
969 }
970 }
971
972 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
973 e = PD->classmeth_end(); i != e; ++i) {
974 ObjCMethodDecl *MD = *i;
975 llvm::Constant *C = GetMethodDescriptionConstant(MD);
976 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
977 OptClassMethods.push_back(C);
978 } else {
979 ClassMethods.push_back(C);
980 }
981 }
982
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000983 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000984 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000985 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000986 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000987 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000988 PD->protocol_begin(),
989 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000990 Values[3] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000991 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
992 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000993 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
994 InstanceMethods);
995 Values[4] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000996 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
997 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000998 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
999 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001000 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1001 Values);
1002
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001003 if (Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001004 // Already created, fix the linkage and update the initializer.
1005 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001006 Entry->setInitializer(Init);
1007 } else {
1008 Entry =
1009 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1010 llvm::GlobalValue::InternalLinkage,
1011 Init,
1012 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1013 &CGM.getModule());
1014 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1015 UsedGlobals.push_back(Entry);
1016 // FIXME: Is this necessary? Why only for protocol?
1017 Entry->setAlignment(4);
1018 }
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001019
1020 return Entry;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001021}
1022
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001023llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001024 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1025
1026 if (!Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001027 // We use the initializer as a marker of whether this is a forward
1028 // reference or not. At module finalization we add the empty
1029 // contents for protocols which were referenced but never defined.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001030 Entry =
1031 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001032 llvm::GlobalValue::ExternalLinkage,
1033 0,
Chris Lattner271d4c22008-11-24 05:29:24 +00001034 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001035 &CGM.getModule());
1036 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1037 UsedGlobals.push_back(Entry);
1038 // FIXME: Is this necessary? Why only for protocol?
1039 Entry->setAlignment(4);
1040 }
1041
1042 return Entry;
1043}
1044
1045/*
1046 struct _objc_protocol_extension {
1047 uint32_t size;
1048 struct objc_method_description_list *optional_instance_methods;
1049 struct objc_method_description_list *optional_class_methods;
1050 struct objc_property_list *instance_properties;
1051 };
1052*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001053llvm::Constant *
1054CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1055 const ConstantVector &OptInstanceMethods,
1056 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001057 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001058 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001059 std::vector<llvm::Constant*> Values(4);
1060 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001061 Values[1] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001062 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1063 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001064 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1065 OptInstanceMethods);
1066 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001067 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1068 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001069 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1070 OptClassMethods);
Chris Lattner271d4c22008-11-24 05:29:24 +00001071 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1072 PD->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001073 0, PD, ObjCTypes);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001074
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001075 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001076 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1077 Values[3]->isNullValue())
1078 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1079
1080 llvm::Constant *Init =
1081 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1082 llvm::GlobalVariable *GV =
1083 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1084 llvm::GlobalValue::InternalLinkage,
1085 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001086 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001087 &CGM.getModule());
1088 // No special section, but goes in llvm.used
1089 UsedGlobals.push_back(GV);
1090
1091 return GV;
1092}
1093
1094/*
1095 struct objc_protocol_list {
1096 struct objc_protocol_list *next;
1097 long count;
1098 Protocol *list[];
1099 };
1100*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001101llvm::Constant *
1102CGObjCMac::EmitProtocolList(const std::string &Name,
1103 ObjCProtocolDecl::protocol_iterator begin,
1104 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001105 std::vector<llvm::Constant*> ProtocolRefs;
1106
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001107 for (; begin != end; ++begin)
1108 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001109
1110 // Just return null for empty protocol lists
1111 if (ProtocolRefs.empty())
1112 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1113
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001114 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001115 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1116
1117 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001118 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001119 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1120 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1121 Values[2] =
1122 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1123 ProtocolRefs.size()),
1124 ProtocolRefs);
1125
1126 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1127 llvm::GlobalVariable *GV =
1128 new llvm::GlobalVariable(Init->getType(), false,
1129 llvm::GlobalValue::InternalLinkage,
1130 Init,
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001131 Name,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001132 &CGM.getModule());
1133 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1134 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1135}
1136
1137/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001138 struct _objc_property {
1139 const char * const name;
1140 const char * const attributes;
1141 };
1142
1143 struct _objc_property_list {
1144 uint32_t entsize; // sizeof (struct _objc_property)
1145 uint32_t prop_count;
1146 struct _objc_property[prop_count];
1147 };
1148*/
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001149llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1150 const Decl *Container,
1151 const ObjCContainerDecl *OCD,
1152 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001153 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroffdcf1e842009-01-11 12:47:58 +00001154 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1155 E = OCD->prop_end(); I != E; ++I) {
1156 const ObjCPropertyDecl *PD = *I;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001157 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001158 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001159 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1160 Prop));
1161 }
1162
1163 // Return null for empty list.
1164 if (Properties.empty())
1165 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1166
1167 unsigned PropertySize =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001168 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001169 std::vector<llvm::Constant*> Values(3);
1170 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1171 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1172 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1173 Properties.size());
1174 Values[2] = llvm::ConstantArray::get(AT, Properties);
1175 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1176
1177 llvm::GlobalVariable *GV =
1178 new llvm::GlobalVariable(Init->getType(), false,
1179 llvm::GlobalValue::InternalLinkage,
1180 Init,
1181 Name,
1182 &CGM.getModule());
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001183 if (ObjCABI == 2)
1184 GV->setSection("__DATA, __objc_const");
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001185 // No special section on property lists?
1186 UsedGlobals.push_back(GV);
1187 return llvm::ConstantExpr::getBitCast(GV,
1188 ObjCTypes.PropertyListPtrTy);
1189
1190}
1191
1192/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001193 struct objc_method_description_list {
1194 int count;
1195 struct objc_method_description list[];
1196 };
1197*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001198llvm::Constant *
1199CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1200 std::vector<llvm::Constant*> Desc(2);
1201 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1202 ObjCTypes.SelectorPtrTy);
1203 Desc[1] = GetMethodVarType(MD);
1204 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1205 Desc);
1206}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001207
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001208llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1209 const char *Section,
1210 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001211 // Return null for empty list.
1212 if (Methods.empty())
1213 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1214
1215 std::vector<llvm::Constant*> Values(2);
1216 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1217 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1218 Methods.size());
1219 Values[1] = llvm::ConstantArray::get(AT, Methods);
1220 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1221
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001222 llvm::GlobalVariable *GV =
1223 new llvm::GlobalVariable(Init->getType(), false,
1224 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001225 Init, Name, &CGM.getModule());
1226 GV->setSection(Section);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001227 UsedGlobals.push_back(GV);
1228 return llvm::ConstantExpr::getBitCast(GV,
1229 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001230}
1231
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001232/*
1233 struct _objc_category {
1234 char *category_name;
1235 char *class_name;
1236 struct _objc_method_list *instance_methods;
1237 struct _objc_method_list *class_methods;
1238 struct _objc_protocol_list *protocols;
1239 uint32_t size; // <rdar://4585769>
1240 struct _objc_property_list *instance_properties;
1241 };
1242 */
Daniel Dunbarac93e472008-08-15 22:20:32 +00001243void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbard8439f22009-01-12 21:08:18 +00001244 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001245
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001246 // FIXME: This is poor design, the OCD should have a pointer to the
1247 // category decl. Additionally, note that Category can be null for
1248 // the @implementation w/o an @interface case. Sema should just
1249 // create one for us as it does for @implementation so everyone else
1250 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001251 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001252 const ObjCCategoryDecl *Category =
1253 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattner271d4c22008-11-24 05:29:24 +00001254 std::string ExtName(Interface->getNameAsString() + "_" +
1255 OCD->getNameAsString());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001256
Daniel Dunbar12996f52008-08-26 21:51:14 +00001257 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1258 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1259 e = OCD->instmeth_end(); i != e; ++i) {
1260 // Instance methods should always be defined.
1261 InstanceMethods.push_back(GetMethodConstant(*i));
1262 }
1263 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1264 e = OCD->classmeth_end(); i != e; ++i) {
1265 // Class methods should always be defined.
1266 ClassMethods.push_back(GetMethodConstant(*i));
1267 }
1268
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001269 std::vector<llvm::Constant*> Values(7);
1270 Values[0] = GetClassName(OCD->getIdentifier());
1271 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001272 Values[2] =
1273 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1274 ExtName,
1275 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001276 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001277 Values[3] =
1278 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1279 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001280 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001281 if (Category) {
1282 Values[4] =
1283 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1284 Category->protocol_begin(),
1285 Category->protocol_end());
1286 } else {
1287 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1288 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001289 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001290
1291 // If there is no category @interface then there can be no properties.
1292 if (Category) {
1293 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001294 OCD, Category, ObjCTypes);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001295 } else {
1296 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1297 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001298
1299 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1300 Values);
1301
1302 llvm::GlobalVariable *GV =
1303 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1304 llvm::GlobalValue::InternalLinkage,
1305 Init,
1306 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1307 &CGM.getModule());
1308 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1309 UsedGlobals.push_back(GV);
1310 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001311}
1312
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001313// FIXME: Get from somewhere?
1314enum ClassFlags {
1315 eClassFlags_Factory = 0x00001,
1316 eClassFlags_Meta = 0x00002,
1317 // <rdr://5142207>
1318 eClassFlags_HasCXXStructors = 0x02000,
1319 eClassFlags_Hidden = 0x20000,
1320 eClassFlags_ABI2_Hidden = 0x00010,
1321 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1322};
1323
1324// <rdr://5142207&4705298&4843145>
1325static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1326 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1327 // FIXME: Support -fvisibility
1328 switch (attr->getVisibility()) {
1329 default:
1330 assert(0 && "Unknown visibility");
1331 return false;
1332 case VisibilityAttr::DefaultVisibility:
1333 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1334 return false;
1335 case VisibilityAttr::HiddenVisibility:
1336 return true;
1337 }
1338 } else {
1339 return false; // FIXME: Support -fvisibility
1340 }
1341}
1342
1343/*
1344 struct _objc_class {
1345 Class isa;
1346 Class super_class;
1347 const char *name;
1348 long version;
1349 long info;
1350 long instance_size;
1351 struct _objc_ivar_list *ivars;
1352 struct _objc_method_list *methods;
1353 struct _objc_cache *cache;
1354 struct _objc_protocol_list *protocols;
1355 // Objective-C 1.0 extensions (<rdr://4585769>)
1356 const char *ivar_layout;
1357 struct _objc_class_ext *ext;
1358 };
1359
1360 See EmitClassExtension();
1361 */
1362void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001363 DefinedSymbols.insert(ID->getIdentifier());
1364
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001365 std::string ClassName = ID->getNameAsString();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001366 // FIXME: Gross
1367 ObjCInterfaceDecl *Interface =
1368 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001369 llvm::Constant *Protocols =
Chris Lattner271d4c22008-11-24 05:29:24 +00001370 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001371 Interface->protocol_begin(),
1372 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001373 const llvm::Type *InterfaceTy =
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001374 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001375 unsigned Flags = eClassFlags_Factory;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001376 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001377
1378 // FIXME: Set CXX-structors flag.
1379 if (IsClassHidden(ID->getClassInterface()))
1380 Flags |= eClassFlags_Hidden;
1381
Daniel Dunbar12996f52008-08-26 21:51:14 +00001382 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1383 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1384 e = ID->instmeth_end(); i != e; ++i) {
1385 // Instance methods should always be defined.
1386 InstanceMethods.push_back(GetMethodConstant(*i));
1387 }
1388 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1389 e = ID->classmeth_end(); i != e; ++i) {
1390 // Class methods should always be defined.
1391 ClassMethods.push_back(GetMethodConstant(*i));
1392 }
1393
1394 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1395 e = ID->propimpl_end(); i != e; ++i) {
1396 ObjCPropertyImplDecl *PID = *i;
1397
1398 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1399 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1400
1401 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1402 if (llvm::Constant *C = GetMethodConstant(MD))
1403 InstanceMethods.push_back(C);
1404 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1405 if (llvm::Constant *C = GetMethodConstant(MD))
1406 InstanceMethods.push_back(C);
1407 }
1408 }
1409
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001410 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar12996f52008-08-26 21:51:14 +00001411 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001412 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001413 // Record a reference to the super class.
1414 LazySymbols.insert(Super->getIdentifier());
1415
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001416 Values[ 1] =
1417 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1418 ObjCTypes.ClassPtrTy);
1419 } else {
1420 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1421 }
1422 Values[ 2] = GetClassName(ID->getIdentifier());
1423 // Version is always 0.
1424 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1425 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1426 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001427 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001428 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001429 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001430 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001431 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001432 // cache is always NULL.
1433 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1434 Values[ 9] = Protocols;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001435 // FIXME: Set ivar_layout
1436 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001437 Values[11] = EmitClassExtension(ID);
1438 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1439 Values);
1440
1441 llvm::GlobalVariable *GV =
1442 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1443 llvm::GlobalValue::InternalLinkage,
1444 Init,
1445 std::string("\01L_OBJC_CLASS_")+ClassName,
1446 &CGM.getModule());
1447 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1448 UsedGlobals.push_back(GV);
1449 // FIXME: Why?
1450 GV->setAlignment(32);
1451 DefinedClasses.push_back(GV);
1452}
1453
1454llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1455 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +00001456 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001457 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001458 unsigned Flags = eClassFlags_Meta;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001459 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001460
1461 if (IsClassHidden(ID->getClassInterface()))
1462 Flags |= eClassFlags_Hidden;
1463
1464 std::vector<llvm::Constant*> Values(12);
1465 // The isa for the metaclass is the root of the hierarchy.
1466 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1467 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1468 Root = Super;
1469 Values[ 0] =
1470 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1471 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001472 // The super class for the metaclass is emitted as the name of the
1473 // super class. The runtime fixes this up to point to the
1474 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001475 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1476 Values[ 1] =
1477 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1478 ObjCTypes.ClassPtrTy);
1479 } else {
1480 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1481 }
1482 Values[ 2] = GetClassName(ID->getIdentifier());
1483 // Version is always 0.
1484 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1485 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1486 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001487 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001488 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001489 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001490 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001491 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001492 // cache is always NULL.
1493 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1494 Values[ 9] = Protocols;
1495 // ivar_layout for metaclass is always NULL.
1496 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1497 // The class extension is always unused for metaclasses.
1498 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1499 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1500 Values);
1501
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001502 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001503 Name += ID->getNameAsCString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001504
1505 // Check for a forward reference.
1506 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1507 if (GV) {
1508 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1509 "Forward metaclass reference has incorrect type.");
1510 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1511 GV->setInitializer(Init);
1512 } else {
1513 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1514 llvm::GlobalValue::InternalLinkage,
1515 Init, Name,
1516 &CGM.getModule());
1517 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001518 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1519 UsedGlobals.push_back(GV);
1520 // FIXME: Why?
1521 GV->setAlignment(32);
1522
1523 return GV;
1524}
1525
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001526llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001527 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001528
1529 // FIXME: Should we look these up somewhere other than the
1530 // module. Its a bit silly since we only generate these while
1531 // processing an implementation, so exactly one pointer would work
1532 // if know when we entered/exitted an implementation block.
1533
1534 // Check for an existing forward reference.
Fariborz Jahanian5fe09f72009-01-07 20:11:22 +00001535 // Previously, metaclass with internal linkage may have been defined.
1536 // pass 'true' as 2nd argument so it is returned.
1537 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001538 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1539 "Forward metaclass reference has incorrect type.");
1540 return GV;
1541 } else {
1542 // Generate as an external reference to keep a consistent
1543 // module. This will be patched up when we emit the metaclass.
1544 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1545 llvm::GlobalValue::ExternalLinkage,
1546 0,
1547 Name,
1548 &CGM.getModule());
1549 }
1550}
1551
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001552/*
1553 struct objc_class_ext {
1554 uint32_t size;
1555 const char *weak_ivar_layout;
1556 struct _objc_property_list *properties;
1557 };
1558*/
1559llvm::Constant *
1560CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1561 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001562 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001563
1564 std::vector<llvm::Constant*> Values(3);
1565 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001566 // FIXME: Output weak_ivar_layout string.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001567 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattner271d4c22008-11-24 05:29:24 +00001568 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001569 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001570
1571 // Return null if no extension bits are used.
1572 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1573 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1574
1575 llvm::Constant *Init =
1576 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1577 llvm::GlobalVariable *GV =
1578 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1579 llvm::GlobalValue::InternalLinkage,
1580 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001581 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001582 &CGM.getModule());
1583 // No special section, but goes in llvm.used
1584 UsedGlobals.push_back(GV);
1585
1586 return GV;
1587}
1588
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001589/// countInheritedIvars - count number of ivars in class and its super class(s)
1590///
1591static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1592 int count = 0;
1593 if (!OI)
1594 return 0;
1595 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1596 if (SuperClass)
1597 count += countInheritedIvars(SuperClass);
1598 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1599 E = OI->ivar_end(); I != E; ++I)
1600 ++count;
1601 return count;
1602}
1603
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00001604/// getInterfaceDeclForIvar - Get the interface declaration node where
1605/// this ivar is declared in.
1606/// FIXME. Ideally, this info should be in the ivar node. But currently
1607/// it is not and prevailing wisdom is that ASTs should not have more
1608/// info than is absolutely needed, even though this info reflects the
1609/// source language.
1610///
1611static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1612 const ObjCInterfaceDecl *OI,
1613 const ObjCIvarDecl *IVD) {
1614 if (!OI)
1615 return 0;
1616 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1617 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1618 E = OI->ivar_end(); I != E; ++I)
1619 if ((*I)->getIdentifier() == IVD->getIdentifier())
1620 return OI;
1621 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1622}
1623
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001624/*
1625 struct objc_ivar {
1626 char *ivar_name;
1627 char *ivar_type;
1628 int ivar_offset;
1629 };
1630
1631 struct objc_ivar_list {
1632 int ivar_count;
1633 struct objc_ivar list[count];
1634 };
1635 */
1636llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001637 bool ForClass) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001638 std::vector<llvm::Constant*> Ivars, Ivar(3);
1639
1640 // When emitting the root class GCC emits ivar entries for the
1641 // actual class structure. It is not clear if we need to follow this
1642 // behavior; for now lets try and get away with not doing it. If so,
1643 // the cleanest solution would be to make up an ObjCInterfaceDecl
1644 // for the class.
1645 if (ForClass)
1646 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001647
1648 ObjCInterfaceDecl *OID =
1649 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1650 const llvm::Type *InterfaceTy =
1651 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001652 const llvm::StructLayout *Layout =
1653 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001654
1655 RecordDecl::field_iterator ifield, pfield;
1656 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001657 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1658 FieldDecl *Field = *ifield;
1659 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1660 getLLVMFieldNo(Field));
1661 if (Field->getIdentifier())
1662 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1663 else
1664 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001665 std::string TypeStr;
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001666 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001667 Ivar[1] = GetMethodVarType(TypeStr);
1668 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001669 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001670 }
1671
1672 // Return null for empty list.
1673 if (Ivars.empty())
1674 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1675
1676 std::vector<llvm::Constant*> Values(2);
1677 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1678 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1679 Ivars.size());
1680 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1681 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1682
1683 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1684 "\01L_OBJC_INSTANCE_VARIABLES_");
1685 llvm::GlobalVariable *GV =
1686 new llvm::GlobalVariable(Init->getType(), false,
1687 llvm::GlobalValue::InternalLinkage,
1688 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001689 Prefix + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001690 &CGM.getModule());
1691 if (ForClass) {
1692 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1693 // FIXME: Why is this only here?
1694 GV->setAlignment(32);
1695 } else {
1696 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1697 }
1698 UsedGlobals.push_back(GV);
1699 return llvm::ConstantExpr::getBitCast(GV,
1700 ObjCTypes.IvarListPtrTy);
1701}
1702
1703/*
1704 struct objc_method {
1705 SEL method_name;
1706 char *method_types;
1707 void *method;
1708 };
1709
1710 struct objc_method_list {
1711 struct objc_method_list *obsolete;
1712 int count;
1713 struct objc_method methods_list[count];
1714 };
1715*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00001716
1717/// GetMethodConstant - Return a struct objc_method constant for the
1718/// given method if it has been defined. The result is null if the
1719/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001720llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001721 // FIXME: Use DenseMap::lookup
1722 llvm::Function *Fn = MethodDefinitions[MD];
1723 if (!Fn)
1724 return 0;
1725
1726 std::vector<llvm::Constant*> Method(3);
1727 Method[0] =
1728 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1729 ObjCTypes.SelectorPtrTy);
1730 Method[1] = GetMethodVarType(MD);
1731 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1732 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1733}
1734
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001735llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1736 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001737 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001738 // Return null for empty list.
1739 if (Methods.empty())
1740 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1741
1742 std::vector<llvm::Constant*> Values(3);
1743 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1744 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1745 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1746 Methods.size());
1747 Values[2] = llvm::ConstantArray::get(AT, Methods);
1748 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1749
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001750 llvm::GlobalVariable *GV =
1751 new llvm::GlobalVariable(Init->getType(), false,
1752 llvm::GlobalValue::InternalLinkage,
1753 Init,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001754 Name,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001755 &CGM.getModule());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001756 GV->setSection(Section);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001757 UsedGlobals.push_back(GV);
1758 return llvm::ConstantExpr::getBitCast(GV,
1759 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00001760}
1761
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001762llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001763 const ObjCContainerDecl *CD) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001764 std::string Name;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001765 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarace33292008-08-16 03:19:19 +00001766
Daniel Dunbar34bda882009-02-02 23:23:47 +00001767 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001768 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001769 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarace33292008-08-16 03:19:19 +00001770 llvm::Function *Method =
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001771 llvm::Function::Create(MethodTy,
Daniel Dunbarace33292008-08-16 03:19:19 +00001772 llvm::GlobalValue::InternalLinkage,
1773 Name,
1774 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00001775 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00001776
Daniel Dunbarace33292008-08-16 03:19:19 +00001777 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001778}
1779
1780llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001781 // Abuse this interface function as a place to finalize.
1782 FinishModule();
1783
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001784 return NULL;
1785}
1786
Daniel Dunbarf7103722008-09-24 03:38:44 +00001787llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1788 return ObjCTypes.GetPropertyFn;
1789}
1790
1791llvm::Function *CGObjCMac::GetPropertySetFunction() {
1792 return ObjCTypes.SetPropertyFn;
1793}
1794
Anders Carlsson58d16242008-08-31 04:05:03 +00001795llvm::Function *CGObjCMac::EnumerationMutationFunction()
1796{
1797 return ObjCTypes.EnumerationMutationFn;
1798}
1799
Daniel Dunbar83544842008-09-28 01:03:14 +00001800/*
1801
1802Objective-C setjmp-longjmp (sjlj) Exception Handling
1803--
1804
1805The basic framework for a @try-catch-finally is as follows:
1806{
1807 objc_exception_data d;
1808 id _rethrow = null;
Anders Carlsson8559de12009-02-07 21:26:04 +00001809 bool _call_try_exit = true;
1810
Daniel Dunbar83544842008-09-28 01:03:14 +00001811 objc_exception_try_enter(&d);
1812 if (!setjmp(d.jmp_buf)) {
1813 ... try body ...
1814 } else {
1815 // exception path
1816 id _caught = objc_exception_extract(&d);
1817
1818 // enter new try scope for handlers
1819 if (!setjmp(d.jmp_buf)) {
1820 ... match exception and execute catch blocks ...
1821
1822 // fell off end, rethrow.
1823 _rethrow = _caught;
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001824 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001825 } else {
1826 // exception in catch block
1827 _rethrow = objc_exception_extract(&d);
Anders Carlsson8559de12009-02-07 21:26:04 +00001828 _call_try_exit = false;
1829 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001830 }
1831 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001832 ... jump-through-finally to finally_end ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001833
1834finally:
Anders Carlsson8559de12009-02-07 21:26:04 +00001835 if (_call_try_exit)
1836 objc_exception_try_exit(&d);
1837
Daniel Dunbar83544842008-09-28 01:03:14 +00001838 ... finally block ....
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001839 ... dispatch to finally destination ...
1840
1841finally_rethrow:
1842 objc_exception_throw(_rethrow);
1843
1844finally_end:
Daniel Dunbar83544842008-09-28 01:03:14 +00001845}
1846
1847This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001848uses _rethrow to determine if objc_exception_try_exit should be called
1849and if the object should be rethrown. This breaks in the face of
1850throwing nil and introduces unnecessary branches.
Daniel Dunbar83544842008-09-28 01:03:14 +00001851
1852We specialize this framework for a few particular circumstances:
1853
1854 - If there are no catch blocks, then we avoid emitting the second
1855 exception handling context.
1856
1857 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1858 e)) we avoid emitting the code to rethrow an uncaught exception.
1859
1860 - FIXME: If there is no @finally block we can do a few more
1861 simplifications.
1862
1863Rethrows and Jumps-Through-Finally
1864--
1865
1866Support for implicit rethrows and jumping through the finally block is
1867handled by storing the current exception-handling context in
1868ObjCEHStack.
1869
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001870In order to implement proper @finally semantics, we support one basic
1871mechanism for jumping through the finally block to an arbitrary
1872destination. Constructs which generate exits from a @try or @catch
1873block use this mechanism to implement the proper semantics by chaining
1874jumps, as necessary.
1875
1876This mechanism works like the one used for indirect goto: we
1877arbitrarily assign an ID to each destination and store the ID for the
1878destination in a variable prior to entering the finally block. At the
1879end of the finally block we simply create a switch to the proper
1880destination.
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001881
1882Code gen for @synchronized(expr) stmt;
1883Effectively generating code for:
1884objc_sync_enter(expr);
1885@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar83544842008-09-28 01:03:14 +00001886*/
1887
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001888void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1889 const Stmt &S) {
1890 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001891 // Create various blocks we refer to for handling @finally.
Daniel Dunbar72f96552008-11-11 02:29:29 +00001892 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson8559de12009-02-07 21:26:04 +00001893 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar72f96552008-11-11 02:29:29 +00001894 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1895 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1896 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001897
1898 // Push an EH context entry, used for handling rethrows and jumps
1899 // through finally.
Anders Carlsson00ffb962009-02-09 20:38:58 +00001900 CGF.PushCleanupBlock(FinallyBlock);
1901
Anders Carlssonecd81832009-02-07 21:37:21 +00001902 CGF.ObjCEHValueStack.push_back(0);
1903
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001904 // Allocate memory for the exception data and rethrow pointer.
Anders Carlssonfca6c292008-09-09 17:59:25 +00001905 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1906 "exceptiondata.ptr");
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001907 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1908 "_rethrow");
Anders Carlsson8559de12009-02-07 21:26:04 +00001909 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1910 "_call_try_exit");
1911 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1912
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001913 if (!isTry) {
1914 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanian3895d922008-11-21 19:21:53 +00001915 llvm::Value *Arg = CGF.EmitScalarExpr(
1916 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1917 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1918 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001919 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00001920
1921 // Enter a new try block and call setjmp.
1922 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1923 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1924 "jmpbufarray");
1925 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1926 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1927 JmpBufPtr, "result");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001928
Daniel Dunbar72f96552008-11-11 02:29:29 +00001929 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1930 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001931 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001932 TryHandler, TryBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001933
1934 // Emit the @try block.
1935 CGF.EmitBlock(TryBlock);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001936 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1937 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00001938 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001939
1940 // Emit the "exception in @try" block.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001941 CGF.EmitBlock(TryHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001942
1943 // Retrieve the exception object. We may emit multiple blocks but
1944 // nothing can cross this so the value is already in SSA form.
1945 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1946 ExceptionData,
1947 "caught");
Anders Carlssonecd81832009-02-07 21:37:21 +00001948 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001949 if (!isTry)
1950 {
1951 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00001952 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00001953 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001954 }
1955 else if (const ObjCAtCatchStmt* CatchStmt =
1956 cast<ObjCAtTryStmt>(S).getCatchStmts())
1957 {
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001958 // Enter a new exception try block (in case a @catch block throws
1959 // an exception).
Anders Carlssonfca6c292008-09-09 17:59:25 +00001960 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001961
Anders Carlssonfca6c292008-09-09 17:59:25 +00001962 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1963 JmpBufPtr, "result");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001964 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001965
Daniel Dunbar72f96552008-11-11 02:29:29 +00001966 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1967 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001968 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001969
1970 CGF.EmitBlock(CatchBlock);
1971
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001972 // Handle catch list. As a special case we check if everything is
1973 // matched and avoid generating code for falling off the end if
1974 // so.
1975 bool AllMatched = false;
Anders Carlssonfca6c292008-09-09 17:59:25 +00001976 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar72f96552008-11-11 02:29:29 +00001977 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001978
Anders Carlsson75d86732008-09-11 09:15:33 +00001979 const DeclStmt *CatchParam =
1980 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001981 const VarDecl *VD = 0;
1982 const PointerType *PT = 0;
1983
Anders Carlssonfca6c292008-09-09 17:59:25 +00001984 // catch(...) always matches.
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001985 if (!CatchParam) {
1986 AllMatched = true;
1987 } else {
Ted Kremenekf41e9f72008-10-06 20:58:56 +00001988 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001989 PT = VD->getType()->getAsPointerType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00001990
Daniel Dunbard04c9352008-09-27 22:21:14 +00001991 // catch(id e) always matches.
1992 // FIXME: For the time being we also match id<X>; this should
1993 // be rejected by Sema instead.
Steve Naroff17c03822009-02-12 17:52:19 +00001994 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbard04c9352008-09-27 22:21:14 +00001995 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001996 AllMatched = true;
Anders Carlssonfca6c292008-09-09 17:59:25 +00001997 }
1998
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001999 if (AllMatched) {
Anders Carlsson75d86732008-09-11 09:15:33 +00002000 if (CatchParam) {
2001 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002002 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002003 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00002004 }
Anders Carlsson1f4acc32008-09-11 08:21:54 +00002005
Anders Carlsson75d86732008-09-11 09:15:33 +00002006 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002007 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002008 break;
2009 }
2010
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002011 assert(PT && "Unexpected non-pointer type in @catch");
2012 QualType T = PT->getPointeeType();
Anders Carlssona4519172008-09-11 06:35:14 +00002013 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002014 assert(ObjCType && "Catch parameter must have Objective-C type!");
2015
2016 // Check if the @catch block matches the exception object.
2017 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2018
Anders Carlssonfca6c292008-09-09 17:59:25 +00002019 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2020 Class, Caught, "match");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002021
Daniel Dunbar72f96552008-11-11 02:29:29 +00002022 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002023
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002024 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002025 MatchedBlock, NextCatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002026
2027 // Emit the @catch block.
2028 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar83544842008-09-28 01:03:14 +00002029 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002030 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar83544842008-09-28 01:03:14 +00002031
2032 llvm::Value *Tmp =
2033 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2034 "tmp");
2035 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00002036
2037 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002038 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002039
2040 CGF.EmitBlock(NextCatchBlock);
2041 }
2042
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002043 if (!AllMatched) {
2044 // None of the handlers caught the exception, so store it to be
2045 // rethrown at the end of the @finally block.
2046 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002047 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002048 }
2049
2050 // Emit the exception handler for the @catch blocks.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002051 CGF.EmitBlock(CatchHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002052 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2053 ExceptionData),
2054 RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002055 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002056 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002057 } else {
Anders Carlssonfca6c292008-09-09 17:59:25 +00002058 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002059 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002060 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002061 }
2062
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002063 // Pop the exception-handling stack entry. It is important to do
2064 // this now, because the code in the @finally block is not in this
2065 // context.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002066 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2067
Anders Carlssonecd81832009-02-07 21:37:21 +00002068 CGF.ObjCEHValueStack.pop_back();
2069
Anders Carlssonfca6c292008-09-09 17:59:25 +00002070 // Emit the @finally block.
2071 CGF.EmitBlock(FinallyBlock);
Anders Carlsson8559de12009-02-07 21:26:04 +00002072 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2073
2074 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2075
2076 CGF.EmitBlock(FinallyExit);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002077 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002078
2079 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002080 if (isTry) {
2081 if (const ObjCAtFinallyStmt* FinallyStmt =
2082 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2083 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2084 }
Fariborz Jahanian3895d922008-11-21 19:21:53 +00002085 else {
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002086 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanian3895d922008-11-21 19:21:53 +00002087 // For @synchronized, call objc_sync_enter(sync.expr)
2088 llvm::Value *Arg = CGF.EmitScalarExpr(
2089 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2090 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2091 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2092 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00002093
Anders Carlsson00ffb962009-02-09 20:38:58 +00002094 // Emit the switch block
2095 if (Info.SwitchBlock)
2096 CGF.EmitBlock(Info.SwitchBlock);
2097 if (Info.EndBlock)
2098 CGF.EmitBlock(Info.EndBlock);
2099
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002100 CGF.EmitBlock(FinallyRethrow);
2101 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2102 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002103 CGF.Builder.CreateUnreachable();
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002104
2105 CGF.EmitBlock(FinallyEnd);
Anders Carlssonb01a2112008-09-09 10:04:29 +00002106}
2107
2108void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002109 const ObjCAtThrowStmt &S) {
Anders Carlsson05d7be72008-09-09 16:16:55 +00002110 llvm::Value *ExceptionAsObject;
2111
2112 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2113 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2114 ExceptionAsObject =
2115 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2116 } else {
Anders Carlssonecd81832009-02-07 21:37:21 +00002117 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar83544842008-09-28 01:03:14 +00002118 "Unexpected rethrow outside @catch block.");
Anders Carlssonecd81832009-02-07 21:37:21 +00002119 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson05d7be72008-09-09 16:16:55 +00002120 }
2121
2122 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002123 CGF.Builder.CreateUnreachable();
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002124
2125 // Clear the insertion point to indicate we are in unreachable code.
2126 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00002127}
2128
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002129/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002130/// object: objc_read_weak (id *src)
2131///
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002132llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002133 llvm::Value *AddrWeakObj)
2134{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002135 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002136 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002137 AddrWeakObj, "weakread");
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002138 return read_weak;
2139}
2140
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002141/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2142/// objc_assign_weak (id src, id *dst)
2143///
2144void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2145 llvm::Value *src, llvm::Value *dst)
2146{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002147 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2148 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002149 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2150 src, dst, "weakassign");
2151 return;
2152}
2153
Fariborz Jahanian17958902008-11-19 00:59:10 +00002154/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2155/// objc_assign_global (id src, id *dst)
2156///
2157void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2158 llvm::Value *src, llvm::Value *dst)
2159{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002160 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2161 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002162 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2163 src, dst, "globalassign");
2164 return;
2165}
2166
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002167/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2168/// objc_assign_ivar (id src, id *dst)
2169///
2170void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2171 llvm::Value *src, llvm::Value *dst)
2172{
2173 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2174 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2175 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2176 src, dst, "assignivar");
2177 return;
2178}
2179
Fariborz Jahanian17958902008-11-19 00:59:10 +00002180/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2181/// objc_assign_strongCast (id src, id *dst)
2182///
2183void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2184 llvm::Value *src, llvm::Value *dst)
2185{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002186 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2187 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002188 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2189 src, dst, "weakassign");
2190 return;
2191}
2192
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002193/// EmitObjCValueForIvar - Code Gen for ivar reference.
2194///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002195LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2196 QualType ObjectTy,
2197 llvm::Value *BaseValue,
2198 const ObjCIvarDecl *Ivar,
2199 const FieldDecl *Field,
2200 unsigned CVRQualifiers) {
2201 if (Ivar->isBitField())
2202 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2203 CVRQualifiers);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002204 // TODO: Add a special case for isa (index 0)
2205 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2206 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002207 LValue LV = LValue::MakeAddr(V,
2208 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2209 LValue::SetObjCIvar(LV, true);
2210 return LV;
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002211}
2212
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002213llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2214 ObjCInterfaceDecl *Interface,
2215 const ObjCIvarDecl *Ivar) {
2216 const llvm::Type *InterfaceLTy =
2217 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2218 const llvm::StructLayout *Layout =
2219 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2220 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2221 uint64_t Offset =
2222 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2223
2224 return llvm::ConstantInt::get(
2225 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2226 Offset);
2227}
2228
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002229/* *** Private Interface *** */
2230
2231/// EmitImageInfo - Emit the image info marker used to encode some module
2232/// level information.
2233///
2234/// See: <rdr://4810609&4810587&4810587>
2235/// struct IMAGE_INFO {
2236/// unsigned version;
2237/// unsigned flags;
2238/// };
2239enum ImageInfoFlags {
2240 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2241 eImageInfo_GarbageCollected = (1 << 1),
2242 eImageInfo_GCOnly = (1 << 2)
2243};
2244
2245void CGObjCMac::EmitImageInfo() {
2246 unsigned version = 0; // Version is unused?
2247 unsigned flags = 0;
2248
2249 // FIXME: Fix and continue?
2250 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2251 flags |= eImageInfo_GarbageCollected;
2252 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2253 flags |= eImageInfo_GCOnly;
2254
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002255 // Emitted as int[2];
2256 llvm::Constant *values[2] = {
2257 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2258 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2259 };
2260 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002261 llvm::GlobalVariable *GV =
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002262 new llvm::GlobalVariable(AT, true,
2263 llvm::GlobalValue::InternalLinkage,
2264 llvm::ConstantArray::get(AT, values, 2),
2265 "\01L_OBJC_IMAGE_INFO",
2266 &CGM.getModule());
2267
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002268 if (ObjCABI == 1) {
2269 GV->setSection("__OBJC, __image_info,regular");
2270 } else {
2271 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2272 }
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002273
2274 UsedGlobals.push_back(GV);
2275}
2276
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002277
2278// struct objc_module {
2279// unsigned long version;
2280// unsigned long size;
2281// const char *name;
2282// Symtab symtab;
2283// };
2284
2285// FIXME: Get from somewhere
2286static const int ModuleVersion = 7;
2287
2288void CGObjCMac::EmitModuleInfo() {
Daniel Dunbard8439f22009-01-12 21:08:18 +00002289 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002290
2291 std::vector<llvm::Constant*> Values(4);
2292 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2293 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00002294 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002295 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002296 Values[3] = EmitModuleSymbols();
2297
2298 llvm::GlobalVariable *GV =
2299 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2300 llvm::GlobalValue::InternalLinkage,
2301 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2302 Values),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002303 "\01L_OBJC_MODULES",
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002304 &CGM.getModule());
2305 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2306 UsedGlobals.push_back(GV);
2307}
2308
2309llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002310 unsigned NumClasses = DefinedClasses.size();
2311 unsigned NumCategories = DefinedCategories.size();
2312
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002313 // Return null if no symbols were defined.
2314 if (!NumClasses && !NumCategories)
2315 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2316
2317 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002318 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2319 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2320 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2321 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2322
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002323 // The runtime expects exactly the list of defined classes followed
2324 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002325 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002326 for (unsigned i=0; i<NumClasses; i++)
2327 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2328 ObjCTypes.Int8PtrTy);
2329 for (unsigned i=0; i<NumCategories; i++)
2330 Symbols[NumClasses + i] =
2331 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2332 ObjCTypes.Int8PtrTy);
2333
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002334 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002335 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002336 NumClasses + NumCategories),
2337 Symbols);
2338
2339 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2340
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002341 llvm::GlobalVariable *GV =
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002342 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002343 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002344 Init,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002345 "\01L_OBJC_SYMBOLS",
2346 &CGM.getModule());
2347 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2348 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002349 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2350}
2351
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002352llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002353 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002354 LazySymbols.insert(ID->getIdentifier());
2355
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002356 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2357
2358 if (!Entry) {
2359 llvm::Constant *Casted =
2360 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2361 ObjCTypes.ClassPtrTy);
2362 Entry =
2363 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2364 llvm::GlobalValue::InternalLinkage,
2365 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2366 &CGM.getModule());
2367 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2368 UsedGlobals.push_back(Entry);
2369 }
2370
2371 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002372}
2373
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002374llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002375 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2376
2377 if (!Entry) {
2378 llvm::Constant *Casted =
2379 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2380 ObjCTypes.SelectorPtrTy);
2381 Entry =
2382 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2383 llvm::GlobalValue::InternalLinkage,
2384 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2385 &CGM.getModule());
2386 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2387 UsedGlobals.push_back(Entry);
2388 }
2389
2390 return Builder.CreateLoad(Entry, false, "tmp");
2391}
2392
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00002393llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002394 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002395
2396 if (!Entry) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002397 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002398 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002399 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002400 llvm::GlobalValue::InternalLinkage,
2401 C, "\01L_OBJC_CLASS_NAME_",
2402 &CGM.getModule());
2403 Entry->setSection("__TEXT,__cstring,cstring_literals");
2404 UsedGlobals.push_back(Entry);
2405 }
2406
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002407 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002408}
2409
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002410llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002411 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2412
2413 if (!Entry) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00002414 // FIXME: Avoid std::string copying.
2415 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002416 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002417 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002418 llvm::GlobalValue::InternalLinkage,
2419 C, "\01L_OBJC_METH_VAR_NAME_",
2420 &CGM.getModule());
2421 Entry->setSection("__TEXT,__cstring,cstring_literals");
2422 UsedGlobals.push_back(Entry);
2423 }
2424
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002425 return getConstantGEP(Entry, 0, 0);
2426}
2427
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002428// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002429llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002430 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2431}
2432
2433// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002434llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002435 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2436}
2437
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002438llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002439 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002440
2441 if (!Entry) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002442 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002443 Entry =
2444 new llvm::GlobalVariable(C->getType(), false,
2445 llvm::GlobalValue::InternalLinkage,
2446 C, "\01L_OBJC_METH_VAR_TYPE_",
2447 &CGM.getModule());
2448 Entry->setSection("__TEXT,__cstring,cstring_literals");
2449 UsedGlobals.push_back(Entry);
2450 }
2451
2452 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002453}
2454
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002455// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002456llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002457 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00002458 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2459 TypeStr);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002460 return GetMethodVarType(TypeStr);
2461}
2462
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002463// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002464llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002465 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2466
2467 if (!Entry) {
2468 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2469 Entry =
2470 new llvm::GlobalVariable(C->getType(), false,
2471 llvm::GlobalValue::InternalLinkage,
2472 C, "\01L_OBJC_PROP_NAME_ATTR_",
2473 &CGM.getModule());
2474 Entry->setSection("__TEXT,__cstring,cstring_literals");
2475 UsedGlobals.push_back(Entry);
2476 }
2477
2478 return getConstantGEP(Entry, 0, 0);
2479}
2480
2481// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002482// FIXME: This Decl should be more precise.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002483llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002484 const Decl *Container) {
2485 std::string TypeStr;
2486 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002487 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2488}
2489
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002490void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2491 const ObjCContainerDecl *CD,
2492 std::string &NameOut) {
Daniel Dunbarace33292008-08-16 03:19:19 +00002493 // FIXME: Find the mangling GCC uses.
Douglas Gregor5d764842009-01-09 17:18:27 +00002494 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner3a8f2942008-11-24 03:33:13 +00002495 NameOut += '[';
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00002496 assert (CD && "Missing container decl in GetNameForMethod");
2497 NameOut += CD->getNameAsString();
Fariborz Jahanianc6f0c212009-01-26 23:49:05 +00002498 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2499 // Right now! there is not enough info. to do this.
Chris Lattner3a8f2942008-11-24 03:33:13 +00002500 NameOut += ' ';
2501 NameOut += D->getSelector().getAsString();
2502 NameOut += ']';
Daniel Dunbarace33292008-08-16 03:19:19 +00002503}
2504
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002505/// GetFirstIvarInRecord - This routine returns the record for the
2506/// implementation of the fiven class OID. It also returns field
2507/// corresponding to the first ivar in the class in FIV. It also
2508/// returns the one before the first ivar.
2509///
2510const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2511 const ObjCInterfaceDecl *OID,
2512 RecordDecl::field_iterator &FIV,
2513 RecordDecl::field_iterator &PIV) {
2514 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2515 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2516 RecordDecl::field_iterator ifield = RD->field_begin();
2517 RecordDecl::field_iterator pfield = RD->field_end();
2518 while (countSuperClassIvars-- > 0) {
2519 pfield = ifield;
2520 ++ifield;
2521 }
2522 FIV = ifield;
2523 PIV = pfield;
2524 return RD;
2525}
2526
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002527void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002528 EmitModuleInfo();
2529
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002530 // Emit the dummy bodies for any protocols which were referenced but
2531 // never defined.
2532 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2533 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2534 if (i->second->hasInitializer())
2535 continue;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002536
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002537 std::vector<llvm::Constant*> Values(5);
2538 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2539 Values[1] = GetClassName(i->first);
2540 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2541 Values[3] = Values[4] =
2542 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2543 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2544 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2545 Values));
2546 }
2547
2548 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002549 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002550 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002551 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002552 }
2553
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002554 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002555 llvm::GlobalValue *GV =
2556 new llvm::GlobalVariable(AT, false,
2557 llvm::GlobalValue::AppendingLinkage,
2558 llvm::ConstantArray::get(AT, Used),
2559 "llvm.used",
2560 &CGM.getModule());
2561
2562 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002563
2564 // Add assembler directives to add lazy undefined symbol references
2565 // for classes which are referenced but not defined. This is
2566 // important for correct linker interaction.
2567
2568 // FIXME: Uh, this isn't particularly portable.
2569 std::stringstream s;
Anders Carlsson63f98352008-12-10 02:21:04 +00002570
2571 if (!CGM.getModule().getModuleInlineAsm().empty())
2572 s << "\n";
2573
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002574 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2575 e = LazySymbols.end(); i != e; ++i) {
2576 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2577 }
2578 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2579 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002580 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002581 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2582 }
Anders Carlsson63f98352008-12-10 02:21:04 +00002583
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002584 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002585}
2586
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002587CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002588 : CGObjCCommonMac(cgm),
2589 ObjCTypes(cgm)
2590{
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00002591 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002592 ObjCABI = 2;
2593}
2594
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002595/* *** */
2596
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002597ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2598: CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002599{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002600 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2601 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002602
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002603 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002604 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002605 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002606 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2607
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002608 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00002609 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002610 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002611
2612 // FIXME: It would be nice to unify this with the opaque type, so
2613 // that the IR comes out a bit cleaner.
2614 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2615 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002616
2617 // I'm not sure I like this. The implicit coordination is a bit
2618 // gross. We should solve this in a reasonable fashion because this
2619 // is a pretty common task (match some runtime data structure with
2620 // an LLVM data structure).
2621
2622 // FIXME: This is leaked.
2623 // FIXME: Merge with rewriter code?
2624
2625 // struct _objc_super {
2626 // id self;
2627 // Class cls;
2628 // }
2629 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2630 SourceLocation(),
2631 &Ctx.Idents.get("_objc_super"));
2632 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2633 Ctx.getObjCIdType(), 0, false));
2634 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2635 Ctx.getObjCClassType(), 0, false));
2636 RD->completeDefinition(Ctx);
2637
2638 SuperCTy = Ctx.getTagDeclType(RD);
2639 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2640
2641 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002642 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2643
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002644 // struct _prop_t {
2645 // char *name;
2646 // char *attributes;
2647 // }
2648 PropertyTy = llvm::StructType::get(Int8PtrTy,
2649 Int8PtrTy,
2650 NULL);
2651 CGM.getModule().addTypeName("struct._prop_t",
2652 PropertyTy);
2653
2654 // struct _prop_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002655 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002656 // uint32_t count_of_properties;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002657 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002658 // }
2659 PropertyListTy = llvm::StructType::get(IntTy,
2660 IntTy,
2661 llvm::ArrayType::get(PropertyTy, 0),
2662 NULL);
2663 CGM.getModule().addTypeName("struct._prop_list_t",
2664 PropertyListTy);
2665 // struct _prop_list_t *
2666 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2667
2668 // struct _objc_method {
2669 // SEL _cmd;
2670 // char *method_type;
2671 // char *_imp;
2672 // }
2673 MethodTy = llvm::StructType::get(SelectorPtrTy,
2674 Int8PtrTy,
2675 Int8PtrTy,
2676 NULL);
2677 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002678
2679 // struct _objc_cache *
2680 CacheTy = llvm::OpaqueType::get();
2681 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2682 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002683
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002684 // Property manipulation functions.
Daniel Dunbardac29922009-02-04 00:44:42 +00002685
2686 QualType IdType = Ctx.getObjCIdType();
2687 QualType SelType = Ctx.getObjCSelType();
2688 llvm::SmallVector<QualType,16> Params;
2689 const llvm::FunctionType *FTy;
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002690
2691 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbardac29922009-02-04 00:44:42 +00002692 Params.push_back(IdType);
2693 Params.push_back(SelType);
2694 Params.push_back(Ctx.LongTy);
2695 Params.push_back(Ctx.BoolTy);
2696 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2697 false);
2698 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002699
2700 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2701 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002702 Params.push_back(IdType);
2703 Params.push_back(SelType);
2704 Params.push_back(Ctx.LongTy);
2705 Params.push_back(IdType);
2706 Params.push_back(Ctx.BoolTy);
2707 Params.push_back(Ctx.BoolTy);
2708 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2709 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2710
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002711 // Enumeration mutation.
Daniel Dunbardac29922009-02-04 00:44:42 +00002712
2713 // void objc_enumerationMutation (id)
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002714 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002715 Params.push_back(IdType);
2716 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2717 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2718 "objc_enumerationMutation");
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002719
2720 // gc's API
2721 // id objc_read_weak (id *)
2722 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002723 Params.push_back(Ctx.getPointerType(IdType));
2724 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2725 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2726
2727 // id objc_assign_weak (id, id *)
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002728 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002729 Params.push_back(IdType);
2730 Params.push_back(Ctx.getPointerType(IdType));
2731
2732 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2733 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2734 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2735 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2736 GcAssignStrongCastFn =
2737 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002738}
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002739
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002740ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2741 : ObjCCommonTypesHelper(cgm)
2742{
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002743 // struct _objc_method_description {
2744 // SEL name;
2745 // char *types;
2746 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002747 MethodDescriptionTy =
2748 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002749 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002750 NULL);
2751 CGM.getModule().addTypeName("struct._objc_method_description",
2752 MethodDescriptionTy);
2753
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002754 // struct _objc_method_description_list {
2755 // int count;
2756 // struct _objc_method_description[1];
2757 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002758 MethodDescriptionListTy =
2759 llvm::StructType::get(IntTy,
2760 llvm::ArrayType::get(MethodDescriptionTy, 0),
2761 NULL);
2762 CGM.getModule().addTypeName("struct._objc_method_description_list",
2763 MethodDescriptionListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002764
2765 // struct _objc_method_description_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002766 MethodDescriptionListPtrTy =
2767 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2768
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002769 // Protocol description structures
2770
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002771 // struct _objc_protocol_extension {
2772 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2773 // struct _objc_method_description_list *optional_instance_methods;
2774 // struct _objc_method_description_list *optional_class_methods;
2775 // struct _objc_property_list *instance_properties;
2776 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002777 ProtocolExtensionTy =
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002778 llvm::StructType::get(IntTy,
2779 MethodDescriptionListPtrTy,
2780 MethodDescriptionListPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002781 PropertyListPtrTy,
2782 NULL);
2783 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2784 ProtocolExtensionTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002785
2786 // struct _objc_protocol_extension *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002787 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2788
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002789 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002790
2791 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2792 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2793
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002794 const llvm::Type *T =
2795 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2796 LongTy,
2797 llvm::ArrayType::get(ProtocolTyHolder, 0),
2798 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002799 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2800
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002801 // struct _objc_protocol {
2802 // struct _objc_protocol_extension *isa;
2803 // char *protocol_name;
2804 // struct _objc_protocol **_objc_protocol_list;
2805 // struct _objc_method_description_list *instance_methods;
2806 // struct _objc_method_description_list *class_methods;
2807 // }
2808 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002809 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002810 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2811 MethodDescriptionListPtrTy,
2812 MethodDescriptionListPtrTy,
2813 NULL);
2814 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2815
2816 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2817 CGM.getModule().addTypeName("struct._objc_protocol_list",
2818 ProtocolListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002819 // struct _objc_protocol_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002820 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2821
2822 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002823 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002824 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002825
2826 // Class description structures
2827
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002828 // struct _objc_ivar {
2829 // char *ivar_name;
2830 // char *ivar_type;
2831 // int ivar_offset;
2832 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002833 IvarTy = llvm::StructType::get(Int8PtrTy,
2834 Int8PtrTy,
2835 IntTy,
2836 NULL);
2837 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2838
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002839 // struct _objc_ivar_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002840 IvarListTy = llvm::OpaqueType::get();
2841 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2842 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2843
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002844 // struct _objc_method_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002845 MethodListTy = llvm::OpaqueType::get();
2846 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2847 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2848
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002849 // struct _objc_class_extension *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002850 ClassExtensionTy =
2851 llvm::StructType::get(IntTy,
2852 Int8PtrTy,
2853 PropertyListPtrTy,
2854 NULL);
2855 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2856 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2857
2858 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2859
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002860 // struct _objc_class {
2861 // Class isa;
2862 // Class super_class;
2863 // char *name;
2864 // long version;
2865 // long info;
2866 // long instance_size;
2867 // struct _objc_ivar_list *ivars;
2868 // struct _objc_method_list *methods;
2869 // struct _objc_cache *cache;
2870 // struct _objc_protocol_list *protocols;
2871 // char *ivar_layout;
2872 // struct _objc_class_ext *ext;
2873 // };
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002874 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2875 llvm::PointerType::getUnqual(ClassTyHolder),
2876 Int8PtrTy,
2877 LongTy,
2878 LongTy,
2879 LongTy,
2880 IvarListPtrTy,
2881 MethodListPtrTy,
2882 CachePtrTy,
2883 ProtocolListPtrTy,
2884 Int8PtrTy,
2885 ClassExtensionPtrTy,
2886 NULL);
2887 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2888
2889 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2890 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2891 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2892
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002893 // struct _objc_category {
2894 // char *category_name;
2895 // char *class_name;
2896 // struct _objc_method_list *instance_method;
2897 // struct _objc_method_list *class_method;
2898 // uint32_t size; // sizeof(struct _objc_category)
2899 // struct _objc_property_list *instance_properties;// category's @property
2900 // }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002901 CategoryTy = llvm::StructType::get(Int8PtrTy,
2902 Int8PtrTy,
2903 MethodListPtrTy,
2904 MethodListPtrTy,
2905 ProtocolListPtrTy,
2906 IntTy,
2907 PropertyListPtrTy,
2908 NULL);
2909 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2910
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002911 // Global metadata structures
2912
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002913 // struct _objc_symtab {
2914 // long sel_ref_cnt;
2915 // SEL *refs;
2916 // short cls_def_cnt;
2917 // short cat_def_cnt;
2918 // char *defs[cls_def_cnt + cat_def_cnt];
2919 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002920 SymtabTy = llvm::StructType::get(LongTy,
2921 SelectorPtrTy,
2922 ShortTy,
2923 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002924 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002925 NULL);
2926 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2927 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2928
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002929 // struct _objc_module {
2930 // long version;
2931 // long size; // sizeof(struct _objc_module)
2932 // char *name;
2933 // struct _objc_symtab* symtab;
2934 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002935 ModuleTy =
2936 llvm::StructType::get(LongTy,
2937 LongTy,
2938 Int8PtrTy,
2939 SymtabPtrTy,
2940 NULL);
2941 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002942
Daniel Dunbarf7103722008-09-24 03:38:44 +00002943 // Message send functions.
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002944
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002945 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002946 std::vector<const llvm::Type*> Params;
2947 Params.push_back(ObjectPtrTy);
2948 Params.push_back(SelectorPtrTy);
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002949 MessageSendFn =
2950 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2951 Params,
2952 true),
2953 "objc_msgSend");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002954
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002955 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002956 Params.clear();
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002957 Params.push_back(ObjectPtrTy);
2958 Params.push_back(SelectorPtrTy);
2959 MessageSendStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002960 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2961 Params,
2962 true),
2963 "objc_msgSend_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002964
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002965 //
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002966 Params.clear();
2967 Params.push_back(ObjectPtrTy);
2968 Params.push_back(SelectorPtrTy);
2969 // FIXME: This should be long double on x86_64?
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002970 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002971 MessageSendFpretFn =
2972 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2973 Params,
2974 true),
2975 "objc_msgSend_fpret");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002976
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002977 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002978 Params.clear();
2979 Params.push_back(SuperPtrTy);
2980 Params.push_back(SelectorPtrTy);
2981 MessageSendSuperFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002982 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2983 Params,
2984 true),
2985 "objc_msgSendSuper");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002986
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002987 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2988 // SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002989 Params.clear();
2990 Params.push_back(Int8PtrTy);
2991 Params.push_back(SuperPtrTy);
2992 Params.push_back(SelectorPtrTy);
2993 MessageSendSuperStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002994 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2995 Params,
2996 true),
2997 "objc_msgSendSuper_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002998
2999 // There is no objc_msgSendSuper_fpret? How can that work?
3000 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson58d16242008-08-31 04:05:03 +00003001
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003002 // FIXME: This is the size of the setjmp buffer and should be
3003 // target specific. 18 is what's used on 32-bit X86.
3004 uint64_t SetJmpBufferSize = 18;
3005
3006 // Exceptions
3007 const llvm::Type *StackPtrTy =
Daniel Dunbar1c5e4632008-09-27 06:32:25 +00003008 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003009
3010 ExceptionDataTy =
3011 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3012 SetJmpBufferSize),
3013 StackPtrTy, NULL);
3014 CGM.getModule().addTypeName("struct._objc_exception_data",
3015 ExceptionDataTy);
3016
3017 Params.clear();
3018 Params.push_back(ObjectPtrTy);
3019 ExceptionThrowFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003020 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3021 Params,
3022 false),
3023 "objc_exception_throw");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003024
3025 Params.clear();
3026 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3027 ExceptionTryEnterFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003028 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3029 Params,
3030 false),
3031 "objc_exception_try_enter");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003032 ExceptionTryExitFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003033 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3034 Params,
3035 false),
3036 "objc_exception_try_exit");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003037 ExceptionExtractFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003038 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3039 Params,
3040 false),
3041 "objc_exception_extract");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003042
3043 Params.clear();
3044 Params.push_back(ClassPtrTy);
3045 Params.push_back(ObjectPtrTy);
3046 ExceptionMatchFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003047 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3048 Params,
3049 false),
3050 "objc_exception_match");
Chris Lattnerdd978702008-11-15 21:26:17 +00003051
3052 // synchronized APIs
3053 // void objc_sync_enter (id)
3054 Params.clear();
3055 Params.push_back(ObjectPtrTy);
3056 SyncEnterFn =
3057 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3058 Params,
3059 false),
3060 "objc_sync_enter");
3061 // void objc_sync_exit (id)
3062 SyncExitFn =
3063 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3064 Params,
3065 false),
3066 "objc_sync_exit");
3067
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003068
3069 Params.clear();
3070 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3071 SetJmpFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003072 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3073 Params,
3074 false),
3075 "_setjmp");
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00003076
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003077}
3078
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003079ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003080: ObjCCommonTypesHelper(cgm)
3081{
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003082 // struct _method_list_t {
3083 // uint32_t entsize; // sizeof(struct _objc_method)
3084 // uint32_t method_count;
3085 // struct _objc_method method_list[method_count];
3086 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003087 MethodListnfABITy = llvm::StructType::get(IntTy,
3088 IntTy,
3089 llvm::ArrayType::get(MethodTy, 0),
3090 NULL);
3091 CGM.getModule().addTypeName("struct.__method_list_t",
3092 MethodListnfABITy);
3093 // struct method_list_t *
3094 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003095
3096 // struct _protocol_t {
3097 // id isa; // NULL
3098 // const char * const protocol_name;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003099 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003100 // const struct method_list_t * const instance_methods;
3101 // const struct method_list_t * const class_methods;
3102 // const struct method_list_t *optionalInstanceMethods;
3103 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003104 // const struct _prop_list_t * properties;
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003105 // const uint32_t size; // sizeof(struct _protocol_t)
3106 // const uint32_t flags; // = 0
3107 // }
3108
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003109 // Holder for struct _protocol_list_t *
3110 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3111
3112 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3113 Int8PtrTy,
3114 llvm::PointerType::getUnqual(
3115 ProtocolListTyHolder),
3116 MethodListnfABIPtrTy,
3117 MethodListnfABIPtrTy,
3118 MethodListnfABIPtrTy,
3119 MethodListnfABIPtrTy,
3120 PropertyListPtrTy,
3121 IntTy,
3122 IntTy,
3123 NULL);
3124 CGM.getModule().addTypeName("struct._protocol_t",
3125 ProtocolnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003126
3127 // struct _protocol_t*
3128 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003129
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003130 // struct _protocol_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003131 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003132 // struct _protocol_t *[protocol_count];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003133 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003134 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3135 llvm::ArrayType::get(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003136 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003137 NULL);
3138 CGM.getModule().addTypeName("struct._objc_protocol_list",
3139 ProtocolListnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003140 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3141 ProtocolListnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003142
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003143 // struct _objc_protocol_list*
3144 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003145
3146 // struct _ivar_t {
3147 // unsigned long int *offset; // pointer to ivar offset location
3148 // char *name;
3149 // char *type;
3150 // uint32_t alignment;
3151 // uint32_t size;
3152 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003153 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3154 Int8PtrTy,
3155 Int8PtrTy,
3156 IntTy,
3157 IntTy,
3158 NULL);
3159 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3160
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003161 // struct _ivar_list_t {
3162 // uint32 entsize; // sizeof(struct _ivar_t)
3163 // uint32 count;
3164 // struct _iver_t list[count];
3165 // }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003166 IvarListnfABITy = llvm::StructType::get(IntTy,
3167 IntTy,
3168 llvm::ArrayType::get(
3169 IvarnfABITy, 0),
3170 NULL);
3171 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3172
3173 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003174
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003175 // struct _class_ro_t {
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003176 // uint32_t const flags;
3177 // uint32_t const instanceStart;
3178 // uint32_t const instanceSize;
3179 // uint32_t const reserved; // only when building for 64bit targets
3180 // const uint8_t * const ivarLayout;
3181 // const char *const name;
3182 // const struct _method_list_t * const baseMethods;
3183 // const struct _objc_protocol_list *const baseProtocols;
3184 // const struct _ivar_list_t *const ivars;
3185 // const uint8_t * const weakIvarLayout;
3186 // const struct _prop_list_t * const properties;
3187 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003188
3189 // FIXME. Add 'reserved' field in 64bit abi mode!
3190 ClassRonfABITy = llvm::StructType::get(IntTy,
3191 IntTy,
3192 IntTy,
3193 Int8PtrTy,
3194 Int8PtrTy,
3195 MethodListnfABIPtrTy,
3196 ProtocolListnfABIPtrTy,
3197 IvarListnfABIPtrTy,
3198 Int8PtrTy,
3199 PropertyListPtrTy,
3200 NULL);
3201 CGM.getModule().addTypeName("struct._class_ro_t",
3202 ClassRonfABITy);
3203
3204 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3205 std::vector<const llvm::Type*> Params;
3206 Params.push_back(ObjectPtrTy);
3207 Params.push_back(SelectorPtrTy);
3208 ImpnfABITy = llvm::PointerType::getUnqual(
3209 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3210
3211 // struct _class_t {
3212 // struct _class_t *isa;
3213 // struct _class_t * const superclass;
3214 // void *cache;
3215 // IMP *vtable;
3216 // struct class_ro_t *ro;
3217 // }
3218
3219 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3220 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3221 llvm::PointerType::getUnqual(ClassTyHolder),
3222 CachePtrTy,
3223 llvm::PointerType::getUnqual(ImpnfABITy),
3224 llvm::PointerType::getUnqual(
3225 ClassRonfABITy),
3226 NULL);
3227 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3228
3229 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3230 ClassnfABITy);
3231
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003232 // LLVM for struct _class_t *
3233 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3234
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003235 // struct _category_t {
3236 // const char * const name;
3237 // struct _class_t *const cls;
3238 // const struct _method_list_t * const instance_methods;
3239 // const struct _method_list_t * const class_methods;
3240 // const struct _protocol_list_t * const protocols;
3241 // const struct _prop_list_t * const properties;
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003242 // }
3243 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003244 ClassnfABIPtrTy,
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003245 MethodListnfABIPtrTy,
3246 MethodListnfABIPtrTy,
3247 ProtocolListnfABIPtrTy,
3248 PropertyListPtrTy,
3249 NULL);
3250 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003251
3252 // New types for nonfragile abi messaging.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3254 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003255
3256 // MessageRefTy - LLVM for:
3257 // struct _message_ref_t {
3258 // IMP messenger;
3259 // SEL name;
3260 // };
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003261
3262 // First the clang type for struct _message_ref_t
3263 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3264 SourceLocation(),
3265 &Ctx.Idents.get("_message_ref_t"));
3266 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3267 Ctx.VoidPtrTy, 0, false));
3268 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3269 Ctx.getObjCSelType(), 0, false));
3270 RD->completeDefinition(Ctx);
3271
3272 MessageRefCTy = Ctx.getTagDeclType(RD);
3273 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3274 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003275
3276 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3277 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3278
3279 // SuperMessageRefTy - LLVM for:
3280 // struct _super_message_ref_t {
3281 // SUPER_IMP messenger;
3282 // SEL name;
3283 // };
3284 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3285 SelectorPtrTy,
3286 NULL);
3287 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3288
3289 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3290 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3291
3292 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3293 Params.clear();
3294 Params.push_back(ObjectPtrTy);
3295 Params.push_back(MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00003296 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3297 Params,
3298 true);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003299 MessageSendFixupFn =
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00003300 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003301 "objc_msgSend_fixup");
3302
3303 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3304 MessageSendFpretFixupFn =
3305 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3306 Params,
3307 true),
3308 "objc_msgSend_fpret_fixup");
3309
3310 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3311 MessageSendStretFixupFn =
3312 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3313 Params,
3314 true),
3315 "objc_msgSend_stret_fixup");
3316
3317 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3318 MessageSendIdFixupFn =
3319 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3320 Params,
3321 true),
3322 "objc_msgSendId_fixup");
3323
3324
3325 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3326 MessageSendIdStretFixupFn =
3327 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3328 Params,
3329 true),
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003330 "objc_msgSendId_stret_fixup");
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003331
3332 // id objc_msgSendSuper2_fixup (struct objc_super *,
3333 // struct _super_message_ref_t*, ...)
3334 Params.clear();
3335 Params.push_back(SuperPtrTy);
3336 Params.push_back(SuperMessageRefPtrTy);
3337 MessageSendSuper2FixupFn =
3338 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3339 Params,
3340 true),
3341 "objc_msgSendSuper2_fixup");
3342
3343
3344 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3345 // struct _super_message_ref_t*, ...)
3346 MessageSendSuper2StretFixupFn =
3347 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3348 Params,
3349 true),
3350 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003351
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003352}
3353
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003354llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3355 FinishNonFragileABIModule();
3356
3357 return NULL;
3358}
3359
3360void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3361 // nonfragile abi has no module definition.
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003362
3363 // Build list of all implemented classe addresses in array
3364 // L_OBJC_LABEL_CLASS_$.
3365 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3366 // list of 'nonlazy' implementations (defined as those with a +load{}
3367 // method!!).
3368 unsigned NumClasses = DefinedClasses.size();
3369 if (NumClasses) {
3370 std::vector<llvm::Constant*> Symbols(NumClasses);
3371 for (unsigned i=0; i<NumClasses; i++)
3372 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3373 ObjCTypes.Int8PtrTy);
3374 llvm::Constant* Init =
3375 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3376 NumClasses),
3377 Symbols);
3378
3379 llvm::GlobalVariable *GV =
3380 new llvm::GlobalVariable(Init->getType(), false,
3381 llvm::GlobalValue::InternalLinkage,
3382 Init,
3383 "\01L_OBJC_LABEL_CLASS_$",
3384 &CGM.getModule());
3385 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3386 UsedGlobals.push_back(GV);
3387 }
3388
3389 // Build list of all implemented category addresses in array
3390 // L_OBJC_LABEL_CATEGORY_$.
3391 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3392 // list of 'nonlazy' category implementations (defined as those with a +load{}
3393 // method!!).
3394 unsigned NumCategory = DefinedCategories.size();
3395 if (NumCategory) {
3396 std::vector<llvm::Constant*> Symbols(NumCategory);
3397 for (unsigned i=0; i<NumCategory; i++)
3398 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3399 ObjCTypes.Int8PtrTy);
3400 llvm::Constant* Init =
3401 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3402 NumCategory),
3403 Symbols);
3404
3405 llvm::GlobalVariable *GV =
3406 new llvm::GlobalVariable(Init->getType(), false,
3407 llvm::GlobalValue::InternalLinkage,
3408 Init,
3409 "\01L_OBJC_LABEL_CATEGORY_$",
3410 &CGM.getModule());
3411 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3412 UsedGlobals.push_back(GV);
3413 }
3414
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00003415 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3416 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3417 std::vector<llvm::Constant*> Values(2);
3418 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3419 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3420 llvm::Constant* Init = llvm::ConstantArray::get(
3421 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3422 Values);
3423 llvm::GlobalVariable *IMGV =
3424 new llvm::GlobalVariable(Init->getType(), false,
3425 llvm::GlobalValue::InternalLinkage,
3426 Init,
3427 "\01L_OBJC_IMAGE_INFO",
3428 &CGM.getModule());
3429 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3430 UsedGlobals.push_back(IMGV);
3431
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003432 std::vector<llvm::Constant*> Used;
3433 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3434 e = UsedGlobals.end(); i != e; ++i) {
3435 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3436 }
3437
3438 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3439 llvm::GlobalValue *GV =
3440 new llvm::GlobalVariable(AT, false,
3441 llvm::GlobalValue::AppendingLinkage,
3442 llvm::ConstantArray::get(AT, Used),
3443 "llvm.used",
3444 &CGM.getModule());
3445
3446 GV->setSection("llvm.metadata");
3447
3448}
3449
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003450// Metadata flags
3451enum MetaDataDlags {
3452 CLS = 0x0,
3453 CLS_META = 0x1,
3454 CLS_ROOT = 0x2,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003455 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003456 CLS_EXCEPTION = 0x20
3457};
3458/// BuildClassRoTInitializer - generate meta-data for:
3459/// struct _class_ro_t {
3460/// uint32_t const flags;
3461/// uint32_t const instanceStart;
3462/// uint32_t const instanceSize;
3463/// uint32_t const reserved; // only when building for 64bit targets
3464/// const uint8_t * const ivarLayout;
3465/// const char *const name;
3466/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003467/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003468/// const struct _ivar_list_t *const ivars;
3469/// const uint8_t * const weakIvarLayout;
3470/// const struct _prop_list_t * const properties;
3471/// }
3472///
3473llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3474 unsigned flags,
3475 unsigned InstanceStart,
3476 unsigned InstanceSize,
3477 const ObjCImplementationDecl *ID) {
3478 std::string ClassName = ID->getNameAsString();
3479 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3480 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3481 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3482 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3483 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003484 // FIXME. ivarLayout is currently null!
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003485 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3486 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003487 // const struct _method_list_t * const baseMethods;
3488 std::vector<llvm::Constant*> Methods;
3489 std::string MethodListName("\01l_OBJC_$_");
3490 if (flags & CLS_META) {
3491 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3492 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3493 e = ID->classmeth_end(); i != e; ++i) {
3494 // Class methods should always be defined.
3495 Methods.push_back(GetMethodConstant(*i));
3496 }
3497 } else {
3498 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3499 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3500 e = ID->instmeth_end(); i != e; ++i) {
3501 // Instance methods should always be defined.
3502 Methods.push_back(GetMethodConstant(*i));
3503 }
Fariborz Jahanian78355ec2009-01-28 22:46:49 +00003504 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3505 e = ID->propimpl_end(); i != e; ++i) {
3506 ObjCPropertyImplDecl *PID = *i;
3507
3508 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3509 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3510
3511 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3512 if (llvm::Constant *C = GetMethodConstant(MD))
3513 Methods.push_back(C);
3514 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3515 if (llvm::Constant *C = GetMethodConstant(MD))
3516 Methods.push_back(C);
3517 }
3518 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003519 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003520 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003521 "__DATA, __objc_const", Methods);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003522
3523 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3524 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3525 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3526 + OID->getNameAsString(),
3527 OID->protocol_begin(),
3528 OID->protocol_end());
3529
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003530 if (flags & CLS_META)
3531 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3532 else
3533 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003534 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003535 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003536 if (flags & CLS_META)
3537 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3538 else
3539 Values[ 9] =
3540 EmitPropertyList(
3541 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3542 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003543 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3544 Values);
3545 llvm::GlobalVariable *CLASS_RO_GV =
3546 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3547 llvm::GlobalValue::InternalLinkage,
3548 Init,
3549 (flags & CLS_META) ?
3550 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3551 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3552 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003553 CLASS_RO_GV->setAlignment(
3554 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003555 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003556 UsedGlobals.push_back(CLASS_RO_GV);
3557 return CLASS_RO_GV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003558
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003559}
3560
3561/// BuildClassMetaData - This routine defines that to-level meta-data
3562/// for the given ClassName for:
3563/// struct _class_t {
3564/// struct _class_t *isa;
3565/// struct _class_t * const superclass;
3566/// void *cache;
3567/// IMP *vtable;
3568/// struct class_ro_t *ro;
3569/// }
3570///
Fariborz Jahanian06726462009-01-24 21:21:53 +00003571llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3572 std::string &ClassName,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003573 llvm::Constant *IsAGV,
3574 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003575 llvm::Constant *ClassRoGV,
3576 bool HiddenVisibility) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003577 std::vector<llvm::Constant*> Values(5);
3578 Values[0] = IsAGV;
Fariborz Jahanian06726462009-01-24 21:21:53 +00003579 Values[1] = SuperClassGV
3580 ? SuperClassGV
3581 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003582 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3583 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3584 Values[4] = ClassRoGV; // &CLASS_RO_GV
3585 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3586 Values);
3587 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3588 if (GV)
3589 GV->setInitializer(Init);
3590 else
3591 GV =
3592 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3593 llvm::GlobalValue::ExternalLinkage,
3594 Init,
3595 ClassName,
3596 &CGM.getModule());
Fariborz Jahanian7c891592009-01-31 01:07:39 +00003597 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003598 GV->setAlignment(
3599 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003600 if (HiddenVisibility)
3601 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003602 UsedGlobals.push_back(GV);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003603 return GV;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003604}
3605
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003606void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3607 std::string ClassName = ID->getNameAsString();
3608 if (!ObjCEmptyCacheVar) {
3609 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003610 ObjCTypes.CacheTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003611 false,
3612 llvm::GlobalValue::ExternalLinkage,
3613 0,
Fariborz Jahaniane45fbce2009-02-03 17:34:34 +00003614 "\01__objc_empty_cache",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003615 &CGM.getModule());
3616 UsedGlobals.push_back(ObjCEmptyCacheVar);
3617
3618 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003619 ObjCTypes.ImpnfABITy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003620 false,
3621 llvm::GlobalValue::ExternalLinkage,
3622 0,
Fariborz Jahaniane45fbce2009-02-03 17:34:34 +00003623 "\01__objc_empty_vtable",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003624 &CGM.getModule());
3625 UsedGlobals.push_back(ObjCEmptyVtableVar);
3626 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003627 assert(ID->getClassInterface() &&
3628 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003629 uint32_t InstanceStart =
3630 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3631 uint32_t InstanceSize = InstanceStart;
3632 uint32_t flags = CLS_META;
3633 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3634 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003635
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003636 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003637
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003638 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3639 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003640 flags |= OBJC2_CLS_HIDDEN;
3641 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003642 // class is root
3643 flags |= CLS_ROOT;
3644 std::string SuperClassName = ObjCClassName + ClassName;
3645 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003646 if (!SuperClassGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003647 SuperClassGV =
3648 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3649 llvm::GlobalValue::ExternalLinkage,
3650 0,
3651 SuperClassName,
3652 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003653 UsedGlobals.push_back(SuperClassGV);
3654 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003655 std::string IsAClassName = ObjCMetaClassName + ClassName;
3656 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003657 if (!IsAGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003658 IsAGV =
3659 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3660 llvm::GlobalValue::ExternalLinkage,
3661 0,
3662 IsAClassName,
3663 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003664 UsedGlobals.push_back(IsAGV);
3665 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003666 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003667 // Has a root. Current class is not a root.
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003668 std::string RootClassName =
3669 ID->getClassInterface()->getSuperClass()->getNameAsString();
3670 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3671 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003672 if (!SuperClassGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003673 SuperClassGV =
3674 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3675 llvm::GlobalValue::ExternalLinkage,
3676 0,
3677 SuperClassName,
3678 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003679 UsedGlobals.push_back(SuperClassGV);
3680 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003681 IsAGV = SuperClassGV;
3682 }
3683 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3684 InstanceStart,
3685 InstanceSize,ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003686 std::string TClassName = ObjCMetaClassName + ClassName;
3687 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003688 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3689 classIsHidden);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003690
3691 // Metadata for the class
3692 flags = CLS;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003693 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003694 flags |= OBJC2_CLS_HIDDEN;
3695 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003696 flags |= CLS_ROOT;
3697 SuperClassGV = 0;
3698 }
3699 else {
3700 // Has a root. Current class is not a root.
3701 std::string RootClassName =
3702 ID->getClassInterface()->getSuperClass()->getNameAsString();
3703 std::string SuperClassName = ObjCClassName + RootClassName;
3704 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003705 if (!SuperClassGV) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003706 SuperClassGV =
3707 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3708 llvm::GlobalValue::ExternalLinkage,
3709 0,
3710 SuperClassName,
3711 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003712 UsedGlobals.push_back(SuperClassGV);
3713 }
Fariborz Jahanian06726462009-01-24 21:21:53 +00003714 }
3715
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003716 InstanceStart = InstanceSize = 0;
3717 if (ObjCInterfaceDecl *OID =
3718 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3719 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003720 const llvm::Type *InterfaceTy =
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00003721 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003722 const llvm::StructLayout *Layout =
3723 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003724
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003725 RecordDecl::field_iterator firstField, lastField;
3726 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003727
3728 for (RecordDecl::field_iterator e = RD->field_end(),
3729 ifield = firstField; ifield != e; ++ifield)
3730 lastField = ifield;
3731
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003732 if (lastField != RD->field_end()) {
3733 FieldDecl *Field = *lastField;
3734 const llvm::Type *FieldTy =
3735 CGM.getTypes().ConvertTypeForMem(Field->getType());
3736 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3737 InstanceSize = Layout->getElementOffset(
3738 CGM.getTypes().getLLVMFieldNo(Field)) +
3739 Size;
3740 if (firstField == RD->field_end())
3741 InstanceStart = InstanceSize;
3742 else
3743 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3744 getLLVMFieldNo(*firstField));
3745 }
3746 }
Fariborz Jahanian06726462009-01-24 21:21:53 +00003747 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003748 InstanceStart,
3749 InstanceSize,
3750 ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003751
3752 TClassName = ObjCClassName + ClassName;
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003753 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003754 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3755 classIsHidden);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003756 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003757}
3758
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00003759/// GenerateProtocolRef - This routine is called to generate code for
3760/// a protocol reference expression; as in:
3761/// @code
3762/// @protocol(Proto1);
3763/// @endcode
3764/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3765/// which will hold address of the protocol meta-data.
3766///
3767llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3768 const ObjCProtocolDecl *PD) {
3769
3770 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3771 ObjCTypes.ExternalProtocolPtrTy);
3772
3773 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3774 ProtocolName += PD->getNameAsCString();
3775
3776 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3777 if (PTGV)
3778 return Builder.CreateLoad(PTGV, false, "tmp");
3779 PTGV = new llvm::GlobalVariable(
3780 Init->getType(), false,
3781 llvm::GlobalValue::WeakLinkage,
3782 Init,
3783 ProtocolName,
3784 &CGM.getModule());
3785 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3786 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3787 UsedGlobals.push_back(PTGV);
3788 return Builder.CreateLoad(PTGV, false, "tmp");
3789}
3790
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003791/// GenerateCategory - Build metadata for a category implementation.
3792/// struct _category_t {
3793/// const char * const name;
3794/// struct _class_t *const cls;
3795/// const struct _method_list_t * const instance_methods;
3796/// const struct _method_list_t * const class_methods;
3797/// const struct _protocol_list_t * const protocols;
3798/// const struct _prop_list_t * const properties;
3799/// }
3800///
3801void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3802{
3803 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003804 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3805 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003806 "_$_" + OCD->getNameAsString());
3807 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3808
3809 std::vector<llvm::Constant*> Values(6);
3810 Values[0] = GetClassName(OCD->getIdentifier());
3811 // meta-class entry symbol
3812 llvm::GlobalVariable *ClassGV =
3813 CGM.getModule().getGlobalVariable(ExtClassName);
3814 if (!ClassGV)
3815 ClassGV =
3816 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3817 llvm::GlobalValue::ExternalLinkage,
3818 0,
3819 ExtClassName,
3820 &CGM.getModule());
3821 UsedGlobals.push_back(ClassGV);
3822 Values[1] = ClassGV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003823 std::vector<llvm::Constant*> Methods;
3824 std::string MethodListName(Prefix);
3825 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3826 "_$_" + OCD->getNameAsString();
3827
3828 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3829 e = OCD->instmeth_end(); i != e; ++i) {
3830 // Instance methods should always be defined.
3831 Methods.push_back(GetMethodConstant(*i));
3832 }
3833
3834 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003835 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003836 Methods);
3837
3838 MethodListName = Prefix;
3839 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3840 OCD->getNameAsString();
3841 Methods.clear();
3842 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3843 e = OCD->classmeth_end(); i != e; ++i) {
3844 // Class methods should always be defined.
3845 Methods.push_back(GetMethodConstant(*i));
3846 }
3847
3848 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003849 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003850 Methods);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003851 const ObjCCategoryDecl *Category =
3852 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian8c7904b2009-02-13 17:52:22 +00003853 if (Category) {
3854 std::string ExtName(Interface->getNameAsString() + "_$_" +
3855 OCD->getNameAsString());
3856 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3857 + Interface->getNameAsString() + "_$_"
3858 + Category->getNameAsString(),
3859 Category->protocol_begin(),
3860 Category->protocol_end());
3861 Values[5] =
3862 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3863 OCD, Category, ObjCTypes);
3864 }
3865 else {
3866 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3867 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3868 }
3869
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003870 llvm::Constant *Init =
3871 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3872 Values);
3873 llvm::GlobalVariable *GCATV
3874 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3875 false,
3876 llvm::GlobalValue::InternalLinkage,
3877 Init,
3878 ExtCatName,
3879 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003880 GCATV->setAlignment(
3881 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003882 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003883 UsedGlobals.push_back(GCATV);
3884 DefinedCategories.push_back(GCATV);
3885}
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003886
3887/// GetMethodConstant - Return a struct objc_method constant for the
3888/// given method if it has been defined. The result is null if the
3889/// method has not been defined. The return value has type MethodPtrTy.
3890llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3891 const ObjCMethodDecl *MD) {
3892 // FIXME: Use DenseMap::lookup
3893 llvm::Function *Fn = MethodDefinitions[MD];
3894 if (!Fn)
3895 return 0;
3896
3897 std::vector<llvm::Constant*> Method(3);
3898 Method[0] =
3899 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3900 ObjCTypes.SelectorPtrTy);
3901 Method[1] = GetMethodVarType(MD);
3902 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3903 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3904}
3905
3906/// EmitMethodList - Build meta-data for method declarations
3907/// struct _method_list_t {
3908/// uint32_t entsize; // sizeof(struct _objc_method)
3909/// uint32_t method_count;
3910/// struct _objc_method method_list[method_count];
3911/// }
3912///
3913llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3914 const std::string &Name,
3915 const char *Section,
3916 const ConstantVector &Methods) {
3917 // Return null for empty list.
3918 if (Methods.empty())
3919 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3920
3921 std::vector<llvm::Constant*> Values(3);
3922 // sizeof(struct _objc_method)
3923 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3924 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3925 // method_count
3926 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3927 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3928 Methods.size());
3929 Values[2] = llvm::ConstantArray::get(AT, Methods);
3930 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3931
3932 llvm::GlobalVariable *GV =
3933 new llvm::GlobalVariable(Init->getType(), false,
3934 llvm::GlobalValue::InternalLinkage,
3935 Init,
3936 Name,
3937 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003938 GV->setAlignment(
3939 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003940 GV->setSection(Section);
3941 UsedGlobals.push_back(GV);
3942 return llvm::ConstantExpr::getBitCast(GV,
3943 ObjCTypes.MethodListnfABIPtrTy);
3944}
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003945
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003946/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3947/// the given ivar.
3948///
3949llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3950 std::string &Name,
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00003951 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003952 const ObjCIvarDecl *Ivar) {
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00003953 Name += "\01_OBJC_IVAR_$_" +
3954 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003955 + Ivar->getNameAsString();
3956 llvm::GlobalVariable *IvarOffsetGV =
3957 CGM.getModule().getGlobalVariable(Name);
3958 if (!IvarOffsetGV)
3959 IvarOffsetGV =
3960 new llvm::GlobalVariable(ObjCTypes.LongTy,
3961 false,
3962 llvm::GlobalValue::ExternalLinkage,
3963 0,
3964 Name,
3965 &CGM.getModule());
3966 return IvarOffsetGV;
3967}
3968
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003969llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003970 const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003971 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003972 unsigned long int Offset) {
3973
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003974 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003975 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003976 + Ivar->getNameAsString());
Fariborz Jahanian55343922009-02-03 00:09:52 +00003977 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003978
3979 llvm::GlobalVariable *IvarOffsetGV =
3980 CGM.getModule().getGlobalVariable(ExternalName);
3981 if (IvarOffsetGV) {
3982 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003983 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00003984 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003985 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian55343922009-02-03 00:09:52 +00003986 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003987 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003988 return IvarOffsetGV;
3989 }
3990
3991 IvarOffsetGV =
3992 new llvm::GlobalVariable(Init->getType(),
3993 false,
3994 llvm::GlobalValue::ExternalLinkage,
3995 Init,
3996 ExternalName,
3997 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003998 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00003999 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004000 // @private and @package have hidden visibility.
4001 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4002 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4003 if (!globalVisibility)
4004 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004005 else
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004006 if (IsClassHidden(ID))
4007 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004008
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004009 IvarOffsetGV->setSection("__DATA, __objc_const");
4010 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian55343922009-02-03 00:09:52 +00004011 return IvarOffsetGV;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004012}
4013
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004014/// EmitIvarList - Emit the ivar list for the given
4015/// implementation. If ForClass is true the list of class ivars
4016/// (i.e. metaclass ivars) is emitted, otherwise the list of
4017/// interface ivars will be emitted. The return value has type
4018/// IvarListnfABIPtrTy.
4019/// struct _ivar_t {
4020/// unsigned long int *offset; // pointer to ivar offset location
4021/// char *name;
4022/// char *type;
4023/// uint32_t alignment;
4024/// uint32_t size;
4025/// }
4026/// struct _ivar_list_t {
4027/// uint32 entsize; // sizeof(struct _ivar_t)
4028/// uint32 count;
4029/// struct _iver_t list[count];
4030/// }
4031///
4032llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4033 const ObjCImplementationDecl *ID) {
4034
4035 std::vector<llvm::Constant*> Ivars, Ivar(5);
4036
4037 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4038 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4039
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004040 // FIXME. Consolidate this with similar code in GenerateClass.
4041 const llvm::Type *InterfaceTy =
4042 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4043 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004044 const llvm::StructLayout *Layout =
4045 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00004046
4047 RecordDecl::field_iterator i,p;
4048 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004049 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4050
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004051 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004052 FieldDecl *Field = *i;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004053 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4054 getLLVMFieldNo(Field));
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004055 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004056 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004057 if (Field->getIdentifier())
4058 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4059 else
4060 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4061 std::string TypeStr;
4062 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4063 Ivar[2] = GetMethodVarType(TypeStr);
4064 const llvm::Type *FieldTy =
4065 CGM.getTypes().ConvertTypeForMem(Field->getType());
4066 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4067 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4068 Field->getType().getTypePtr()) >> 3;
4069 Align = llvm::Log2_32(Align);
4070 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian71bcf672009-01-27 22:27:56 +00004071 // NOTE. Size of a bitfield does not match gcc's, because of the way
4072 // bitfields are treated special in each. But I am told that 'size'
4073 // for bitfield ivars is ignored by the runtime so it does not matter.
4074 // (even if it matters, some day, there is enough info. to get the bitfield
4075 // right!
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004076 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4077 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4078 }
4079 // Return null for empty list.
4080 if (Ivars.empty())
4081 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4082 std::vector<llvm::Constant*> Values(3);
4083 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4084 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4085 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4086 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4087 Ivars.size());
4088 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4089 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4090 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4091 llvm::GlobalVariable *GV =
4092 new llvm::GlobalVariable(Init->getType(), false,
4093 llvm::GlobalValue::InternalLinkage,
4094 Init,
4095 Prefix + OID->getNameAsString(),
4096 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004097 GV->setAlignment(
4098 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004099 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004100
4101 UsedGlobals.push_back(GV);
4102 return llvm::ConstantExpr::getBitCast(GV,
4103 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004104}
4105
4106llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4107 const ObjCProtocolDecl *PD) {
4108 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4109
4110 if (!Entry) {
4111 // We use the initializer as a marker of whether this is a forward
4112 // reference or not. At module finalization we add the empty
4113 // contents for protocols which were referenced but never defined.
4114 Entry =
4115 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4116 llvm::GlobalValue::ExternalLinkage,
4117 0,
4118 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4119 &CGM.getModule());
4120 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4121 UsedGlobals.push_back(Entry);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004122 }
4123
4124 return Entry;
4125}
4126
4127/// GetOrEmitProtocol - Generate the protocol meta-data:
4128/// @code
4129/// struct _protocol_t {
4130/// id isa; // NULL
4131/// const char * const protocol_name;
4132/// const struct _protocol_list_t * protocol_list; // super protocols
4133/// const struct method_list_t * const instance_methods;
4134/// const struct method_list_t * const class_methods;
4135/// const struct method_list_t *optionalInstanceMethods;
4136/// const struct method_list_t *optionalClassMethods;
4137/// const struct _prop_list_t * properties;
4138/// const uint32_t size; // sizeof(struct _protocol_t)
4139/// const uint32_t flags; // = 0
4140/// }
4141/// @endcode
4142///
4143
4144llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4145 const ObjCProtocolDecl *PD) {
4146 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4147
4148 // Early exit if a defining object has already been generated.
4149 if (Entry && Entry->hasInitializer())
4150 return Entry;
4151
4152 const char *ProtocolName = PD->getNameAsCString();
4153
4154 // Construct method lists.
4155 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4156 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4157 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4158 e = PD->instmeth_end(); i != e; ++i) {
4159 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004160 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004161 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4162 OptInstanceMethods.push_back(C);
4163 } else {
4164 InstanceMethods.push_back(C);
4165 }
4166 }
4167
4168 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4169 e = PD->classmeth_end(); i != e; ++i) {
4170 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004171 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004172 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4173 OptClassMethods.push_back(C);
4174 } else {
4175 ClassMethods.push_back(C);
4176 }
4177 }
4178
4179 std::vector<llvm::Constant*> Values(10);
4180 // isa is NULL
4181 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4182 Values[1] = GetClassName(PD->getIdentifier());
4183 Values[2] = EmitProtocolList(
4184 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4185 PD->protocol_begin(),
4186 PD->protocol_end());
4187
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004188 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004189 + PD->getNameAsString(),
4190 "__DATA, __objc_const",
4191 InstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004192 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004193 + PD->getNameAsString(),
4194 "__DATA, __objc_const",
4195 ClassMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004196 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004197 + PD->getNameAsString(),
4198 "__DATA, __objc_const",
4199 OptInstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004200 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004201 + PD->getNameAsString(),
4202 "__DATA, __objc_const",
4203 OptClassMethods);
4204 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4205 0, PD, ObjCTypes);
4206 uint32_t Size =
4207 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4208 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4209 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4210 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4211 Values);
4212
4213 if (Entry) {
4214 // Already created, fix the linkage and update the initializer.
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004215 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004216 Entry->setInitializer(Init);
4217 } else {
4218 Entry =
4219 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004220 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004221 Init,
4222 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4223 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004224 Entry->setAlignment(
4225 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004226 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004227 }
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004228 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4229
4230 // Use this protocol meta-data to build protocol list table in section
4231 // __DATA, __objc_protolist
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004232 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004233 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004234 llvm::GlobalValue::WeakLinkage,
4235 Entry,
4236 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4237 +ProtocolName,
4238 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004239 PTGV->setAlignment(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004240 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004241 PTGV->setSection("__DATA, __objc_protolist");
4242 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4243 UsedGlobals.push_back(PTGV);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004244 return Entry;
4245}
4246
4247/// EmitProtocolList - Generate protocol list meta-data:
4248/// @code
4249/// struct _protocol_list_t {
4250/// long protocol_count; // Note, this is 32/64 bit
4251/// struct _protocol_t[protocol_count];
4252/// }
4253/// @endcode
4254///
4255llvm::Constant *
4256CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4257 ObjCProtocolDecl::protocol_iterator begin,
4258 ObjCProtocolDecl::protocol_iterator end) {
4259 std::vector<llvm::Constant*> ProtocolRefs;
4260
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004261 // Just return null for empty protocol lists
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004262 if (begin == end)
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004263 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4264
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004265 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004266 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4267 if (GV)
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004268 return llvm::ConstantExpr::getBitCast(GV,
4269 ObjCTypes.ProtocolListnfABIPtrTy);
4270
4271 for (; begin != end; ++begin)
4272 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4273
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004274 // This list is null terminated.
4275 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004276 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004277
4278 std::vector<llvm::Constant*> Values(2);
4279 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4280 Values[1] =
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004281 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004282 ProtocolRefs.size()),
4283 ProtocolRefs);
4284
4285 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4286 GV = new llvm::GlobalVariable(Init->getType(), false,
4287 llvm::GlobalValue::InternalLinkage,
4288 Init,
4289 Name,
4290 &CGM.getModule());
4291 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004292 GV->setAlignment(
4293 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004294 UsedGlobals.push_back(GV);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004295 return llvm::ConstantExpr::getBitCast(GV,
4296 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004297}
4298
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004299/// GetMethodDescriptionConstant - This routine build following meta-data:
4300/// struct _objc_method {
4301/// SEL _cmd;
4302/// char *method_type;
4303/// char *_imp;
4304/// }
4305
4306llvm::Constant *
4307CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4308 std::vector<llvm::Constant*> Desc(3);
4309 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4310 ObjCTypes.SelectorPtrTy);
4311 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004312 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004313 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4314 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4315}
Fariborz Jahanian55343922009-02-03 00:09:52 +00004316
4317/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4318/// This code gen. amounts to generating code for:
4319/// @code
4320/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4321/// @encode
4322///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004323LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004324 CodeGen::CodeGenFunction &CGF,
4325 QualType ObjectTy,
4326 llvm::Value *BaseValue,
4327 const ObjCIvarDecl *Ivar,
4328 const FieldDecl *Field,
4329 unsigned CVRQualifiers) {
4330 assert(ObjectTy->isObjCInterfaceType() &&
4331 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00004332 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004333 std::string ExternalName;
4334 llvm::GlobalVariable *IvarOffsetGV =
4335 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004336
Fariborz Jahanian55343922009-02-03 00:09:52 +00004337 // (char *) BaseValue
4338 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4339 ObjCTypes.Int8PtrTy);
4340 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4341 // (char*)BaseValue + Offset_symbol
4342 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4343 // (type *)((char*)BaseValue + Offset_symbol)
4344 const llvm::Type *IvarTy =
4345 CGM.getTypes().ConvertType(Ivar->getType());
4346 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4347 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004348
4349 if (Ivar->isBitField())
4350 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4351 CVRQualifiers);
4352
4353 LValue LV = LValue::MakeAddr(V,
4354 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4355 LValue::SetObjCIvar(LV, true);
4356 return LV;
Fariborz Jahanian55343922009-02-03 00:09:52 +00004357}
4358
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004359llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4360 CodeGen::CodeGenFunction &CGF,
4361 ObjCInterfaceDecl *Interface,
4362 const ObjCIvarDecl *Ivar) {
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004363 std::string ExternalName;
4364 llvm::GlobalVariable *IvarOffsetGV =
4365 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4366
4367 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004368}
4369
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004370CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4371 CodeGen::CodeGenFunction &CGF,
4372 QualType ResultType,
4373 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004374 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004375 QualType Arg0Ty,
4376 bool IsSuper,
4377 const CallArgList &CallArgs) {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004378 // FIXME. Even though IsSuper is passes. This function doese not
4379 // handle calls to 'super' receivers.
4380 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004381 llvm::Value *Arg0 = Receiver;
4382 if (!IsSuper)
4383 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004384
4385 // Find the message function name.
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004386 // FIXME. This is too much work to get the ABI-specific result type
4387 // needed to find the message name.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004388 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4389 llvm::SmallVector<QualType, 16>());
4390 llvm::Constant *Fn;
4391 std::string Name("\01l_");
4392 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004393#if 0
4394 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004395 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4396 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4397 // FIXME. Is there a better way of getting these names.
4398 // They are available in RuntimeFunctions vector pair.
4399 Name += "objc_msgSendId_stret_fixup";
4400 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004401 else
4402#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004403 if (IsSuper) {
4404 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4405 Name += "objc_msgSendSuper2_stret_fixup";
4406 }
4407 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004408 {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004409 Fn = ObjCTypes.MessageSendStretFixupFn;
4410 Name += "objc_msgSend_stret_fixup";
4411 }
4412 }
Fariborz Jahanianbea03192009-02-05 19:35:43 +00004413 else if (ResultType->isFloatingType() &&
4414 // Selection of frret API only happens in 32bit nonfragile ABI.
4415 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004416 Fn = ObjCTypes.MessageSendFpretFixupFn;
4417 Name += "objc_msgSend_fpret_fixup";
4418 }
4419 else {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004420#if 0
4421// unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004422 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4423 Fn = ObjCTypes.MessageSendIdFixupFn;
4424 Name += "objc_msgSendId_fixup";
4425 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004426 else
4427#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004428 if (IsSuper) {
4429 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4430 Name += "objc_msgSendSuper2_fixup";
4431 }
4432 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004433 {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004434 Fn = ObjCTypes.MessageSendFixupFn;
4435 Name += "objc_msgSend_fixup";
4436 }
4437 }
4438 Name += '_';
4439 std::string SelName(Sel.getAsString());
4440 // Replace all ':' in selector name with '_' ouch!
4441 for(unsigned i = 0; i < SelName.size(); i++)
4442 if (SelName[i] == ':')
4443 SelName[i] = '_';
4444 Name += SelName;
4445 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4446 if (!GV) {
4447 // Build messafe ref table entry.
4448 std::vector<llvm::Constant*> Values(2);
4449 Values[0] = Fn;
4450 Values[1] = GetMethodVarName(Sel);
4451 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4452 GV = new llvm::GlobalVariable(Init->getType(), false,
4453 llvm::GlobalValue::WeakLinkage,
4454 Init,
4455 Name,
4456 &CGM.getModule());
4457 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4458 GV->setAlignment(
4459 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4460 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4461 UsedGlobals.push_back(GV);
4462 }
4463 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004464
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004465 CallArgList ActualArgs;
4466 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4467 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4468 ObjCTypes.MessageRefCPtrTy));
4469 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004470 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4471 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4472 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianf3c17752009-02-14 21:25:36 +00004473 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004474 Callee = CGF.Builder.CreateBitCast(Callee,
4475 llvm::PointerType::getUnqual(FTy));
4476 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004477}
4478
4479/// Generate code for a message send expression in the nonfragile abi.
4480CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4481 CodeGen::CodeGenFunction &CGF,
4482 QualType ResultType,
4483 Selector Sel,
4484 llvm::Value *Receiver,
4485 bool IsClassMessage,
4486 const CallArgList &CallArgs) {
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004487 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004488 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004489 false, CallArgs);
4490}
4491
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004492llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004493 const ObjCInterfaceDecl *ID,
4494 bool IsSuper) {
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004495
4496 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4497
4498 if (!Entry) {
4499 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4500 llvm::GlobalVariable *ClassGV =
4501 CGM.getModule().getGlobalVariable(ClassName);
4502 if (!ClassGV) {
4503 ClassGV =
4504 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4505 llvm::GlobalValue::ExternalLinkage,
4506 0,
4507 ClassName,
4508 &CGM.getModule());
4509 UsedGlobals.push_back(ClassGV);
4510 }
4511 Entry =
4512 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4513 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004514 ClassGV,
4515 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4516 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004517 &CGM.getModule());
4518 Entry->setAlignment(
4519 CGM.getTargetData().getPrefTypeAlignment(
4520 ObjCTypes.ClassnfABIPtrTy));
4521
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004522 if (IsSuper)
4523 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4524 else
4525 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004526 UsedGlobals.push_back(Entry);
4527 }
4528
4529 return Builder.CreateLoad(Entry, false, "tmp");
4530}
4531
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004532/// EmitMetaClassRef - Return a Value * of the address of _class_t
4533/// meta-data
4534///
4535llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4536 const ObjCInterfaceDecl *ID) {
4537 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4538 if (Entry)
4539 return Builder.CreateLoad(Entry, false, "tmp");
4540
4541 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4542 llvm::GlobalVariable *MetaClassGV =
4543 CGM.getModule().getGlobalVariable(MetaClassName);
4544 if (!MetaClassGV) {
4545 MetaClassGV =
4546 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4547 llvm::GlobalValue::ExternalLinkage,
4548 0,
4549 MetaClassName,
4550 &CGM.getModule());
4551 UsedGlobals.push_back(MetaClassGV);
4552 }
4553
4554 Entry =
4555 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4556 llvm::GlobalValue::InternalLinkage,
4557 MetaClassGV,
4558 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4559 &CGM.getModule());
4560 Entry->setAlignment(
4561 CGM.getTargetData().getPrefTypeAlignment(
4562 ObjCTypes.ClassnfABIPtrTy));
4563
4564 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4565 UsedGlobals.push_back(Entry);
4566
4567 return Builder.CreateLoad(Entry, false, "tmp");
4568}
4569
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004570/// GetClass - Return a reference to the class for the given interface
4571/// decl.
4572llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4573 const ObjCInterfaceDecl *ID) {
4574 return EmitClassRef(Builder, ID);
4575}
4576
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004577/// Generates a message send where the super is the receiver. This is
4578/// a message send to self with special delivery semantics indicating
4579/// which class's method should be called.
4580CodeGen::RValue
4581CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4582 QualType ResultType,
4583 Selector Sel,
4584 const ObjCInterfaceDecl *Class,
4585 llvm::Value *Receiver,
4586 bool IsClassMessage,
4587 const CodeGen::CallArgList &CallArgs) {
4588 // ...
4589 // Create and init a super structure; this is a (receiver, class)
4590 // pair we will pass to objc_msgSendSuper.
4591 llvm::Value *ObjCSuper =
4592 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4593
4594 llvm::Value *ReceiverAsObject =
4595 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4596 CGF.Builder.CreateStore(ReceiverAsObject,
4597 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4598
4599 // If this is a class message the metaclass is passed as the target.
4600 llvm::Value *Target =
4601 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4602 : EmitClassRef(CGF.Builder, Class, true);
4603
4604 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4605 // and ObjCTypes types.
4606 const llvm::Type *ClassTy =
4607 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4608 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4609 CGF.Builder.CreateStore(Target,
4610 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4611
4612 return EmitMessageSend(CGF, ResultType, Sel,
4613 ObjCSuper, ObjCTypes.SuperPtrCTy,
4614 true, CallArgs);
4615}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00004616
4617llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4618 Selector Sel) {
4619 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4620
4621 if (!Entry) {
4622 llvm::Constant *Casted =
4623 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4624 ObjCTypes.SelectorPtrTy);
4625 Entry =
4626 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4627 llvm::GlobalValue::InternalLinkage,
4628 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4629 &CGM.getModule());
4630 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4631 UsedGlobals.push_back(Entry);
4632 }
4633
4634 return Builder.CreateLoad(Entry, false, "tmp");
4635}
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00004636/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4637/// objc_assign_ivar (id src, id *dst)
4638///
4639void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4640 llvm::Value *src, llvm::Value *dst)
4641{
4642 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4643 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4644 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4645 src, dst, "assignivar");
4646 return;
4647}
4648
4649/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4650/// objc_assign_strongCast (id src, id *dst)
4651///
4652void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4653 CodeGen::CodeGenFunction &CGF,
4654 llvm::Value *src, llvm::Value *dst)
4655{
4656 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4657 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4658 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4659 src, dst, "weakassign");
4660 return;
4661}
4662
4663/// EmitObjCWeakRead - Code gen for loading value of a __weak
4664/// object: objc_read_weak (id *src)
4665///
4666llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4667 CodeGen::CodeGenFunction &CGF,
4668 llvm::Value *AddrWeakObj)
4669{
4670 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4671 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4672 AddrWeakObj, "weakread");
4673 return read_weak;
4674}
4675
4676/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4677/// objc_assign_weak (id src, id *dst)
4678///
4679void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4680 llvm::Value *src, llvm::Value *dst)
4681{
4682 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4683 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4684 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4685 src, dst, "weakassign");
4686 return;
4687}
4688
4689/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4690/// objc_assign_global (id src, id *dst)
4691///
4692void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4693 llvm::Value *src, llvm::Value *dst)
4694{
4695 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4696 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4697 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4698 src, dst, "globalassign");
4699 return;
4700}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00004701
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00004702/* *** */
4703
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00004704CodeGen::CGObjCRuntime *
4705CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00004706 return new CGObjCMac(CGM);
4707}
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004708
4709CodeGen::CGObjCRuntime *
Fariborz Jahaniand0374812009-01-22 23:02:58 +00004710CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004711 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004712}