blob: eef74083daeab60173201561b688c2d6c08f528c [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000351public:
352 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
353 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000354
355 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000356
357 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
358 const ObjCContainerDecl *CD=0);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000359};
360
361class CGObjCMac : public CGObjCCommonMac {
362private:
363 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000364 /// EmitImageInfo - Emit the image info marker used to encode some module
365 /// level information.
366 void EmitImageInfo();
367
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000368 /// EmitModuleInfo - Another marker encoding module level
369 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000370 void EmitModuleInfo();
371
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000372 /// EmitModuleSymols - Emit module symbols, the list of defined
373 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000374 llvm::Constant *EmitModuleSymbols();
375
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000376 /// FinishModule - Write out global data structures at the end of
377 /// processing a translation unit.
378 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000379
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000380 /// EmitClassExtension - Generate the class extension structure used
381 /// to store the weak ivar layout and properties. The return value
382 /// has type ClassExtensionPtrTy.
383 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
384
385 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
386 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000387 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000388 const ObjCInterfaceDecl *ID);
389
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000390 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000391 QualType ResultType,
392 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000393 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000394 QualType Arg0Ty,
395 bool IsSuper,
396 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000397
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000398 /// EmitIvarList - Emit the ivar list for the given
399 /// implementation. If ForClass is true the list of class ivars
400 /// (i.e. metaclass ivars) is emitted, otherwise the list of
401 /// interface ivars will be emitted. The return value has type
402 /// IvarListPtrTy.
403 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
404 bool ForClass,
405 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000406
407 /// EmitMetaClass - Emit a forward reference to the class structure
408 /// for the metaclass of the given interface. The return value has
409 /// type ClassPtrTy.
410 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
411
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000412 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000413 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000414 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
415 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000416 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000417 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000418
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000419 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000420
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000421 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000422
423 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000424 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000425 llvm::Constant *EmitMethodList(const std::string &Name,
426 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000427 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000428
429 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000430 /// method declarations.
431 /// - TypeName: The name for the type containing the methods.
432 /// - IsProtocol: True iff these methods are for a protocol.
433 /// - ClassMethds: True iff these are class methods.
434 /// - Required: When true, only "required" methods are
435 /// listed. Similarly, when false only "optional" methods are
436 /// listed. For classes this should always be true.
437 /// - begin, end: The method list to output.
438 ///
439 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000440 llvm::Constant *EmitMethodDescList(const std::string &Name,
441 const char *Section,
442 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000443
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000444 /// EmitPropertyList - Emit the given property list. The return
445 /// value has type PropertyListPtrTy.
446 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000447 const Decl *Container,
448 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000449
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000450 /// GetOrEmitProtocol - Get the protocol object for the given
451 /// declaration, emitting it if necessary. The return value has type
452 /// ProtocolPtrTy.
453 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
454
455 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
456 /// object for the given declaration, emitting it if needed. These
457 /// forward references will be filled in with empty bodies if no
458 /// definition is seen. The return value has type ProtocolPtrTy.
459 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
460
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000461 /// EmitProtocolExtension - Generate the protocol extension
462 /// structure used to store optional instance and class methods, and
463 /// protocol properties. The return value has type
464 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000465 llvm::Constant *
466 EmitProtocolExtension(const ObjCProtocolDecl *PD,
467 const ConstantVector &OptInstanceMethods,
468 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000469
470 /// EmitProtocolList - Generate the list of referenced
471 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +0000472 llvm::Constant *EmitProtocolList(const std::string &Name,
473 ObjCProtocolDecl::protocol_iterator begin,
474 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000475
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
477 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000478 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000479
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000480 /// GetProtocolRef - Return a reference to the internal protocol
481 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000482 /// defined. The return value has type ProtocolPtrTy.
483 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000484
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000485public:
486 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000487
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000488 virtual llvm::Function *ModuleInitFunction();
489
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000490 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000491 QualType ResultType,
492 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000493 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000494 bool IsClassMessage,
495 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000496
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000497 virtual CodeGen::RValue
498 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000499 QualType ResultType,
500 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000501 const ObjCInterfaceDecl *Class,
502 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000503 bool IsClassMessage,
504 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000505
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000506 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000507 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000508
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000509 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000510
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000511 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000513 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000514
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000515 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000516 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000517
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000518 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000519
Daniel Dunbar49f66022008-09-24 03:38:44 +0000520 virtual llvm::Function *GetPropertyGetFunction();
521 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000522 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000523
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000524 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
525 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000526 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
527 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000528 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000529 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000530 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
531 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000532 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
533 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000534 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
535 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000536 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
537 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000538};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000539
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000540class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000541private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000542 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000543 llvm::GlobalVariable* ObjCEmptyCacheVar;
544 llvm::GlobalVariable* ObjCEmptyVtableVar;
545
546 /// FinishNonFragileABIModule - Write out global data structures at the end of
547 /// processing a translation unit.
548 void FinishNonFragileABIModule();
549
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000550 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
551 unsigned InstanceStart,
552 unsigned InstanceSize,
553 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000554 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
555 llvm::Constant *IsAGV,
556 llvm::Constant *SuperClassGV,
557 llvm::Constant *ClassRoGV);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000558
559 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
560
561 /// EmitMethodList - Emit the method list for the given
562 /// implementation. The return value has type MethodListnfABITy.
563 llvm::Constant *EmitMethodList(const std::string &Name,
564 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000565 const ConstantVector &Methods);
566 /// EmitIvarList - Emit the ivar list for the given
567 /// implementation. If ForClass is true the list of class ivars
568 /// (i.e. metaclass ivars) is emitted, otherwise the list of
569 /// interface ivars will be emitted. The return value has type
570 /// IvarListnfABIPtrTy.
571 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000572
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000573public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000574 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000575 // FIXME. All stubs for now!
576 virtual llvm::Function *ModuleInitFunction();
577
578 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
579 QualType ResultType,
580 Selector Sel,
581 llvm::Value *Receiver,
582 bool IsClassMessage,
583 const CallArgList &CallArgs)
584 {return RValue::get(0);}
585
586 virtual CodeGen::RValue
587 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
588 QualType ResultType,
589 Selector Sel,
590 const ObjCInterfaceDecl *Class,
591 llvm::Value *Receiver,
592 bool IsClassMessage,
593 const CallArgList &CallArgs){ return RValue::get(0);}
594
595 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
596 const ObjCInterfaceDecl *ID){ return 0; }
597
598 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
599 { return 0; }
600
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000601 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000602
603 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
604
605 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
606 const ObjCProtocolDecl *PD)
607 { return 0; }
608
609 virtual void GenerateProtocol(const ObjCProtocolDecl *PD){ return; }
610
611 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
612 virtual llvm::Function *GetPropertySetFunction()
613 { return 0; }
614 virtual llvm::Function *EnumerationMutationFunction()
615 { return 0; }
616
617 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
618 const Stmt &S)
619 { return; }
620 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
621 const ObjCAtThrowStmt &S)
622 { return; }
623 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
624 llvm::Value *AddrWeakObj)
625 { return 0; }
626 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
627 llvm::Value *src, llvm::Value *dst)
628 { return; }
629 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
630 llvm::Value *src, llvm::Value *dest)
631 { return; }
632 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
633 llvm::Value *src, llvm::Value *dest)
634 { return; }
635 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
636 llvm::Value *src, llvm::Value *dest)
637 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000638};
639
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000640} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000641
642/* *** Helper Functions *** */
643
644/// getConstantGEP() - Help routine to construct simple GEPs.
645static llvm::Constant *getConstantGEP(llvm::Constant *C,
646 unsigned idx0,
647 unsigned idx1) {
648 llvm::Value *Idxs[] = {
649 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
650 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
651 };
652 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
653}
654
655/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000656
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000657CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
658 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000659{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000660 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000661 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000662}
663
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000664/// GetClass - Return a reference to the class for the given interface
665/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000666llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000667 const ObjCInterfaceDecl *ID) {
668 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000669}
670
671/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000672llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000673 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000674}
675
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000676/// Generate a constant CFString object.
677/*
678 struct __builtin_CFString {
679 const int *isa; // point to __CFConstantStringClassReference
680 int flags;
681 const char *str;
682 long length;
683 };
684*/
685
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000686llvm::Constant *CGObjCCommonMac::GenerateConstantString(
687 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000688 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000689}
690
691/// Generates a message send where the super is the receiver. This is
692/// a message send to self with special delivery semantics indicating
693/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000694CodeGen::RValue
695CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000696 QualType ResultType,
697 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000698 const ObjCInterfaceDecl *Class,
699 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000700 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000701 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000702 // Create and init a super structure; this is a (receiver, class)
703 // pair we will pass to objc_msgSendSuper.
704 llvm::Value *ObjCSuper =
705 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
706 llvm::Value *ReceiverAsObject =
707 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
708 CGF.Builder.CreateStore(ReceiverAsObject,
709 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000710
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000711 // If this is a class message the metaclass is passed as the target.
712 llvm::Value *Target;
713 if (IsClassMessage) {
714 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
715 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
716 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
717 Target = Super;
718 } else {
719 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
720 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000721 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
722 // and ObjCTypes types.
723 const llvm::Type *ClassTy =
724 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000725 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000726 CGF.Builder.CreateStore(Target,
727 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
728
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000729 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000730 ObjCSuper, ObjCTypes.SuperPtrCTy,
731 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000732}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000733
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000734/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000735CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000736 QualType ResultType,
737 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000738 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000739 bool IsClassMessage,
740 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000741 llvm::Value *Arg0 =
742 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000743 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000744 Arg0, CGF.getContext().getObjCIdType(),
745 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000746}
747
748CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000749 QualType ResultType,
750 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000751 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000752 QualType Arg0Ty,
753 bool IsSuper,
754 const CallArgList &CallArgs) {
755 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000756 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
757 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
758 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000759 CGF.getContext().getObjCSelType()));
760 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000761
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000762 const llvm::FunctionType *FTy =
763 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
764 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000765
766 llvm::Constant *Fn;
767 if (CGM.ReturnTypeUsesSret(ResultType)) {
768 Fn = ObjCTypes.getSendStretFn(IsSuper);
769 } else if (ResultType->isFloatingType()) {
770 // FIXME: Sadly, this is wrong. This actually depends on the
771 // architecture. This happens to be right for x86-32 though.
772 Fn = ObjCTypes.getSendFpretFn(IsSuper);
773 } else {
774 Fn = ObjCTypes.getSendFn(IsSuper);
775 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000776 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000777 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000778}
779
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000780llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000781 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000782 // FIXME: I don't understand why gcc generates this, or where it is
783 // resolved. Investigate. Its also wasteful to look this up over and
784 // over.
785 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
786
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000787 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
788 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000789}
790
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000791void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
792 // FIXME: We shouldn't need this, the protocol decl should contain
793 // enough information to tell us whether this was a declaration or a
794 // definition.
795 DefinedProtocols.insert(PD->getIdentifier());
796
797 // If we have generated a forward reference to this protocol, emit
798 // it now. Otherwise do nothing, the protocol objects are lazily
799 // emitted.
800 if (Protocols.count(PD->getIdentifier()))
801 GetOrEmitProtocol(PD);
802}
803
804llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
805 if (DefinedProtocols.count(PD->getIdentifier()))
806 return GetOrEmitProtocol(PD);
807 return GetOrEmitProtocolRef(PD);
808}
809
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000810/*
811 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
812 struct _objc_protocol {
813 struct _objc_protocol_extension *isa;
814 char *protocol_name;
815 struct _objc_protocol_list *protocol_list;
816 struct _objc__method_prototype_list *instance_methods;
817 struct _objc__method_prototype_list *class_methods
818 };
819
820 See EmitProtocolExtension().
821*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000822llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
823 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
824
825 // Early exit if a defining object has already been generated.
826 if (Entry && Entry->hasInitializer())
827 return Entry;
828
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000829 // FIXME: I don't understand why gcc generates this, or where it is
830 // resolved. Investigate. Its also wasteful to look this up over and
831 // over.
832 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
833
Chris Lattner8ec03f52008-11-24 03:54:41 +0000834 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000835
836 // Construct method lists.
837 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
838 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
839 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
840 e = PD->instmeth_end(); i != e; ++i) {
841 ObjCMethodDecl *MD = *i;
842 llvm::Constant *C = GetMethodDescriptionConstant(MD);
843 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
844 OptInstanceMethods.push_back(C);
845 } else {
846 InstanceMethods.push_back(C);
847 }
848 }
849
850 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
851 e = PD->classmeth_end(); i != e; ++i) {
852 ObjCMethodDecl *MD = *i;
853 llvm::Constant *C = GetMethodDescriptionConstant(MD);
854 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
855 OptClassMethods.push_back(C);
856 } else {
857 ClassMethods.push_back(C);
858 }
859 }
860
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000861 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000862 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000863 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000864 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000865 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000866 PD->protocol_begin(),
867 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000868 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000869 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
870 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000871 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
872 InstanceMethods);
873 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000874 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
875 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000876 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
877 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000878 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
879 Values);
880
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000881 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000882 // Already created, fix the linkage and update the initializer.
883 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000884 Entry->setInitializer(Init);
885 } else {
886 Entry =
887 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
888 llvm::GlobalValue::InternalLinkage,
889 Init,
890 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
891 &CGM.getModule());
892 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
893 UsedGlobals.push_back(Entry);
894 // FIXME: Is this necessary? Why only for protocol?
895 Entry->setAlignment(4);
896 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000897
898 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000899}
900
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000901llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000902 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
903
904 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000905 // We use the initializer as a marker of whether this is a forward
906 // reference or not. At module finalization we add the empty
907 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 Entry =
909 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000910 llvm::GlobalValue::ExternalLinkage,
911 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000912 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000913 &CGM.getModule());
914 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
915 UsedGlobals.push_back(Entry);
916 // FIXME: Is this necessary? Why only for protocol?
917 Entry->setAlignment(4);
918 }
919
920 return Entry;
921}
922
923/*
924 struct _objc_protocol_extension {
925 uint32_t size;
926 struct objc_method_description_list *optional_instance_methods;
927 struct objc_method_description_list *optional_class_methods;
928 struct objc_property_list *instance_properties;
929 };
930*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000931llvm::Constant *
932CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
933 const ConstantVector &OptInstanceMethods,
934 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000935 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000936 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000937 std::vector<llvm::Constant*> Values(4);
938 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000939 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000940 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
941 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000942 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
943 OptInstanceMethods);
944 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000945 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
946 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000947 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
948 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000949 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
950 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000951 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000952
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000953 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000954 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
955 Values[3]->isNullValue())
956 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
957
958 llvm::Constant *Init =
959 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
960 llvm::GlobalVariable *GV =
961 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
962 llvm::GlobalValue::InternalLinkage,
963 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000964 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000965 &CGM.getModule());
966 // No special section, but goes in llvm.used
967 UsedGlobals.push_back(GV);
968
969 return GV;
970}
971
972/*
973 struct objc_protocol_list {
974 struct objc_protocol_list *next;
975 long count;
976 Protocol *list[];
977 };
978*/
Daniel Dunbardbc93372008-08-21 21:57:41 +0000979llvm::Constant *
980CGObjCMac::EmitProtocolList(const std::string &Name,
981 ObjCProtocolDecl::protocol_iterator begin,
982 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 std::vector<llvm::Constant*> ProtocolRefs;
984
Daniel Dunbardbc93372008-08-21 21:57:41 +0000985 for (; begin != end; ++begin)
986 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987
988 // Just return null for empty protocol lists
989 if (ProtocolRefs.empty())
990 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
991
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000992 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
994
995 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000996 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000997 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
998 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
999 Values[2] =
1000 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1001 ProtocolRefs.size()),
1002 ProtocolRefs);
1003
1004 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1005 llvm::GlobalVariable *GV =
1006 new llvm::GlobalVariable(Init->getType(), false,
1007 llvm::GlobalValue::InternalLinkage,
1008 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001009 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001010 &CGM.getModule());
1011 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1012 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1013}
1014
1015/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001016 struct _objc_property {
1017 const char * const name;
1018 const char * const attributes;
1019 };
1020
1021 struct _objc_property_list {
1022 uint32_t entsize; // sizeof (struct _objc_property)
1023 uint32_t prop_count;
1024 struct _objc_property[prop_count];
1025 };
1026*/
1027llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001028 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +00001029 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001030 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001031 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1032 E = OCD->prop_end(); I != E; ++I) {
1033 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001034 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001035 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001036 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1037 Prop));
1038 }
1039
1040 // Return null for empty list.
1041 if (Properties.empty())
1042 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1043
1044 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001045 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001046 std::vector<llvm::Constant*> Values(3);
1047 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1048 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1049 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1050 Properties.size());
1051 Values[2] = llvm::ConstantArray::get(AT, Properties);
1052 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1053
1054 llvm::GlobalVariable *GV =
1055 new llvm::GlobalVariable(Init->getType(), false,
1056 llvm::GlobalValue::InternalLinkage,
1057 Init,
1058 Name,
1059 &CGM.getModule());
1060 // No special section on property lists?
1061 UsedGlobals.push_back(GV);
1062 return llvm::ConstantExpr::getBitCast(GV,
1063 ObjCTypes.PropertyListPtrTy);
1064
1065}
1066
1067/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001068 struct objc_method_description_list {
1069 int count;
1070 struct objc_method_description list[];
1071 };
1072*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001073llvm::Constant *
1074CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1075 std::vector<llvm::Constant*> Desc(2);
1076 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1077 ObjCTypes.SelectorPtrTy);
1078 Desc[1] = GetMethodVarType(MD);
1079 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1080 Desc);
1081}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001082
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001083llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1084 const char *Section,
1085 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001086 // Return null for empty list.
1087 if (Methods.empty())
1088 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1089
1090 std::vector<llvm::Constant*> Values(2);
1091 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1092 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1093 Methods.size());
1094 Values[1] = llvm::ConstantArray::get(AT, Methods);
1095 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1096
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001097 llvm::GlobalVariable *GV =
1098 new llvm::GlobalVariable(Init->getType(), false,
1099 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 Init, Name, &CGM.getModule());
1101 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001102 UsedGlobals.push_back(GV);
1103 return llvm::ConstantExpr::getBitCast(GV,
1104 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001105}
1106
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001107/*
1108 struct _objc_category {
1109 char *category_name;
1110 char *class_name;
1111 struct _objc_method_list *instance_methods;
1112 struct _objc_method_list *class_methods;
1113 struct _objc_protocol_list *protocols;
1114 uint32_t size; // <rdar://4585769>
1115 struct _objc_property_list *instance_properties;
1116 };
1117 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001118void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001119 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001120
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001121 // FIXME: This is poor design, the OCD should have a pointer to the
1122 // category decl. Additionally, note that Category can be null for
1123 // the @implementation w/o an @interface case. Sema should just
1124 // create one for us as it does for @implementation so everyone else
1125 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001126 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001127 const ObjCCategoryDecl *Category =
1128 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001129 std::string ExtName(Interface->getNameAsString() + "_" +
1130 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001131
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001132 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1133 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1134 e = OCD->instmeth_end(); i != e; ++i) {
1135 // Instance methods should always be defined.
1136 InstanceMethods.push_back(GetMethodConstant(*i));
1137 }
1138 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1139 e = OCD->classmeth_end(); i != e; ++i) {
1140 // Class methods should always be defined.
1141 ClassMethods.push_back(GetMethodConstant(*i));
1142 }
1143
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001144 std::vector<llvm::Constant*> Values(7);
1145 Values[0] = GetClassName(OCD->getIdentifier());
1146 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001147 Values[2] =
1148 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1149 ExtName,
1150 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001151 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001152 Values[3] =
1153 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1154 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001155 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001156 if (Category) {
1157 Values[4] =
1158 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1159 Category->protocol_begin(),
1160 Category->protocol_end());
1161 } else {
1162 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1163 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001164 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001165
1166 // If there is no category @interface then there can be no properties.
1167 if (Category) {
1168 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001169 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001170 } else {
1171 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1172 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001173
1174 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1175 Values);
1176
1177 llvm::GlobalVariable *GV =
1178 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1179 llvm::GlobalValue::InternalLinkage,
1180 Init,
1181 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1182 &CGM.getModule());
1183 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1184 UsedGlobals.push_back(GV);
1185 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001186}
1187
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001188// FIXME: Get from somewhere?
1189enum ClassFlags {
1190 eClassFlags_Factory = 0x00001,
1191 eClassFlags_Meta = 0x00002,
1192 // <rdr://5142207>
1193 eClassFlags_HasCXXStructors = 0x02000,
1194 eClassFlags_Hidden = 0x20000,
1195 eClassFlags_ABI2_Hidden = 0x00010,
1196 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1197};
1198
1199// <rdr://5142207&4705298&4843145>
1200static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1201 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1202 // FIXME: Support -fvisibility
1203 switch (attr->getVisibility()) {
1204 default:
1205 assert(0 && "Unknown visibility");
1206 return false;
1207 case VisibilityAttr::DefaultVisibility:
1208 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1209 return false;
1210 case VisibilityAttr::HiddenVisibility:
1211 return true;
1212 }
1213 } else {
1214 return false; // FIXME: Support -fvisibility
1215 }
1216}
1217
1218/*
1219 struct _objc_class {
1220 Class isa;
1221 Class super_class;
1222 const char *name;
1223 long version;
1224 long info;
1225 long instance_size;
1226 struct _objc_ivar_list *ivars;
1227 struct _objc_method_list *methods;
1228 struct _objc_cache *cache;
1229 struct _objc_protocol_list *protocols;
1230 // Objective-C 1.0 extensions (<rdr://4585769>)
1231 const char *ivar_layout;
1232 struct _objc_class_ext *ext;
1233 };
1234
1235 See EmitClassExtension();
1236 */
1237void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001238 DefinedSymbols.insert(ID->getIdentifier());
1239
Chris Lattner8ec03f52008-11-24 03:54:41 +00001240 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001241 // FIXME: Gross
1242 ObjCInterfaceDecl *Interface =
1243 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001244 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001245 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001246 Interface->protocol_begin(),
1247 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001248 const llvm::Type *InterfaceTy =
1249 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1250 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001251 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001252
1253 // FIXME: Set CXX-structors flag.
1254 if (IsClassHidden(ID->getClassInterface()))
1255 Flags |= eClassFlags_Hidden;
1256
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001257 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1258 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1259 e = ID->instmeth_end(); i != e; ++i) {
1260 // Instance methods should always be defined.
1261 InstanceMethods.push_back(GetMethodConstant(*i));
1262 }
1263 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1264 e = ID->classmeth_end(); i != e; ++i) {
1265 // Class methods should always be defined.
1266 ClassMethods.push_back(GetMethodConstant(*i));
1267 }
1268
1269 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1270 e = ID->propimpl_end(); i != e; ++i) {
1271 ObjCPropertyImplDecl *PID = *i;
1272
1273 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1274 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1275
1276 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1277 if (llvm::Constant *C = GetMethodConstant(MD))
1278 InstanceMethods.push_back(C);
1279 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1280 if (llvm::Constant *C = GetMethodConstant(MD))
1281 InstanceMethods.push_back(C);
1282 }
1283 }
1284
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001285 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001286 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001287 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001288 // Record a reference to the super class.
1289 LazySymbols.insert(Super->getIdentifier());
1290
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001291 Values[ 1] =
1292 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1293 ObjCTypes.ClassPtrTy);
1294 } else {
1295 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1296 }
1297 Values[ 2] = GetClassName(ID->getIdentifier());
1298 // Version is always 0.
1299 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1300 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1301 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1302 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001303 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001304 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001305 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001306 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001307 // cache is always NULL.
1308 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1309 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001310 // FIXME: Set ivar_layout
1311 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001312 Values[11] = EmitClassExtension(ID);
1313 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1314 Values);
1315
1316 llvm::GlobalVariable *GV =
1317 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1318 llvm::GlobalValue::InternalLinkage,
1319 Init,
1320 std::string("\01L_OBJC_CLASS_")+ClassName,
1321 &CGM.getModule());
1322 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1323 UsedGlobals.push_back(GV);
1324 // FIXME: Why?
1325 GV->setAlignment(32);
1326 DefinedClasses.push_back(GV);
1327}
1328
1329llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1330 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001331 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001332 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001333 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001334 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001335
1336 if (IsClassHidden(ID->getClassInterface()))
1337 Flags |= eClassFlags_Hidden;
1338
1339 std::vector<llvm::Constant*> Values(12);
1340 // The isa for the metaclass is the root of the hierarchy.
1341 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1342 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1343 Root = Super;
1344 Values[ 0] =
1345 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1346 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001347 // The super class for the metaclass is emitted as the name of the
1348 // super class. The runtime fixes this up to point to the
1349 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001350 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1351 Values[ 1] =
1352 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1353 ObjCTypes.ClassPtrTy);
1354 } else {
1355 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1356 }
1357 Values[ 2] = GetClassName(ID->getIdentifier());
1358 // Version is always 0.
1359 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1360 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1361 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1362 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001363 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001364 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001365 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001366 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001367 // cache is always NULL.
1368 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1369 Values[ 9] = Protocols;
1370 // ivar_layout for metaclass is always NULL.
1371 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1372 // The class extension is always unused for metaclasses.
1373 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1374 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1375 Values);
1376
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001377 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001378 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001379
1380 // Check for a forward reference.
1381 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1382 if (GV) {
1383 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1384 "Forward metaclass reference has incorrect type.");
1385 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1386 GV->setInitializer(Init);
1387 } else {
1388 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1389 llvm::GlobalValue::InternalLinkage,
1390 Init, Name,
1391 &CGM.getModule());
1392 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001393 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1394 UsedGlobals.push_back(GV);
1395 // FIXME: Why?
1396 GV->setAlignment(32);
1397
1398 return GV;
1399}
1400
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001401llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001402 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001403
1404 // FIXME: Should we look these up somewhere other than the
1405 // module. Its a bit silly since we only generate these while
1406 // processing an implementation, so exactly one pointer would work
1407 // if know when we entered/exitted an implementation block.
1408
1409 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001410 // Previously, metaclass with internal linkage may have been defined.
1411 // pass 'true' as 2nd argument so it is returned.
1412 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001413 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1414 "Forward metaclass reference has incorrect type.");
1415 return GV;
1416 } else {
1417 // Generate as an external reference to keep a consistent
1418 // module. This will be patched up when we emit the metaclass.
1419 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1420 llvm::GlobalValue::ExternalLinkage,
1421 0,
1422 Name,
1423 &CGM.getModule());
1424 }
1425}
1426
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001427/*
1428 struct objc_class_ext {
1429 uint32_t size;
1430 const char *weak_ivar_layout;
1431 struct _objc_property_list *properties;
1432 };
1433*/
1434llvm::Constant *
1435CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1436 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001437 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001438
1439 std::vector<llvm::Constant*> Values(3);
1440 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001441 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001442 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001443 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001444 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001445
1446 // Return null if no extension bits are used.
1447 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1448 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1449
1450 llvm::Constant *Init =
1451 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1452 llvm::GlobalVariable *GV =
1453 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1454 llvm::GlobalValue::InternalLinkage,
1455 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001456 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001457 &CGM.getModule());
1458 // No special section, but goes in llvm.used
1459 UsedGlobals.push_back(GV);
1460
1461 return GV;
1462}
1463
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001464/// countInheritedIvars - count number of ivars in class and its super class(s)
1465///
1466static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1467 int count = 0;
1468 if (!OI)
1469 return 0;
1470 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1471 if (SuperClass)
1472 count += countInheritedIvars(SuperClass);
1473 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1474 E = OI->ivar_end(); I != E; ++I)
1475 ++count;
1476 return count;
1477}
1478
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001479/*
1480 struct objc_ivar {
1481 char *ivar_name;
1482 char *ivar_type;
1483 int ivar_offset;
1484 };
1485
1486 struct objc_ivar_list {
1487 int ivar_count;
1488 struct objc_ivar list[count];
1489 };
1490 */
1491llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1492 bool ForClass,
1493 const llvm::Type *InterfaceTy) {
1494 std::vector<llvm::Constant*> Ivars, Ivar(3);
1495
1496 // When emitting the root class GCC emits ivar entries for the
1497 // actual class structure. It is not clear if we need to follow this
1498 // behavior; for now lets try and get away with not doing it. If so,
1499 // the cleanest solution would be to make up an ObjCInterfaceDecl
1500 // for the class.
1501 if (ForClass)
1502 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1503
1504 const llvm::StructLayout *Layout =
1505 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001506 ObjCInterfaceDecl *OID =
1507 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1508 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1509 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1510 RecordDecl::field_iterator ifield = RD->field_begin();
1511 while (countSuperClassIvars-- > 0)
1512 ++ifield;
1513 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1514 FieldDecl *Field = *ifield;
1515 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1516 getLLVMFieldNo(Field));
1517 if (Field->getIdentifier())
1518 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1519 else
1520 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001521 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001522 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001523 Ivar[1] = GetMethodVarType(TypeStr);
1524 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001525 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001526 }
1527
1528 // Return null for empty list.
1529 if (Ivars.empty())
1530 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1531
1532 std::vector<llvm::Constant*> Values(2);
1533 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1534 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1535 Ivars.size());
1536 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1537 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1538
1539 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1540 "\01L_OBJC_INSTANCE_VARIABLES_");
1541 llvm::GlobalVariable *GV =
1542 new llvm::GlobalVariable(Init->getType(), false,
1543 llvm::GlobalValue::InternalLinkage,
1544 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001545 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001546 &CGM.getModule());
1547 if (ForClass) {
1548 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1549 // FIXME: Why is this only here?
1550 GV->setAlignment(32);
1551 } else {
1552 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1553 }
1554 UsedGlobals.push_back(GV);
1555 return llvm::ConstantExpr::getBitCast(GV,
1556 ObjCTypes.IvarListPtrTy);
1557}
1558
1559/*
1560 struct objc_method {
1561 SEL method_name;
1562 char *method_types;
1563 void *method;
1564 };
1565
1566 struct objc_method_list {
1567 struct objc_method_list *obsolete;
1568 int count;
1569 struct objc_method methods_list[count];
1570 };
1571*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001572
1573/// GetMethodConstant - Return a struct objc_method constant for the
1574/// given method if it has been defined. The result is null if the
1575/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001576llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001577 // FIXME: Use DenseMap::lookup
1578 llvm::Function *Fn = MethodDefinitions[MD];
1579 if (!Fn)
1580 return 0;
1581
1582 std::vector<llvm::Constant*> Method(3);
1583 Method[0] =
1584 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1585 ObjCTypes.SelectorPtrTy);
1586 Method[1] = GetMethodVarType(MD);
1587 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1588 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1589}
1590
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001591llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1592 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001593 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001594 // Return null for empty list.
1595 if (Methods.empty())
1596 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1597
1598 std::vector<llvm::Constant*> Values(3);
1599 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1600 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1601 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1602 Methods.size());
1603 Values[2] = llvm::ConstantArray::get(AT, Methods);
1604 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1605
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001606 llvm::GlobalVariable *GV =
1607 new llvm::GlobalVariable(Init->getType(), false,
1608 llvm::GlobalValue::InternalLinkage,
1609 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001610 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001611 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001612 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001613 UsedGlobals.push_back(GV);
1614 return llvm::ConstantExpr::getBitCast(GV,
1615 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001616}
1617
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001618llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001619 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001620 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001621 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001622
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001623 const llvm::FunctionType *MethodTy =
1624 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001625 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001626 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001627 llvm::GlobalValue::InternalLinkage,
1628 Name,
1629 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001630 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001631
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001632 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001633}
1634
1635llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001636 // Abuse this interface function as a place to finalize.
1637 FinishModule();
1638
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001639 return NULL;
1640}
1641
Daniel Dunbar49f66022008-09-24 03:38:44 +00001642llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1643 return ObjCTypes.GetPropertyFn;
1644}
1645
1646llvm::Function *CGObjCMac::GetPropertySetFunction() {
1647 return ObjCTypes.SetPropertyFn;
1648}
1649
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001650llvm::Function *CGObjCMac::EnumerationMutationFunction()
1651{
1652 return ObjCTypes.EnumerationMutationFn;
1653}
1654
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001655/*
1656
1657Objective-C setjmp-longjmp (sjlj) Exception Handling
1658--
1659
1660The basic framework for a @try-catch-finally is as follows:
1661{
1662 objc_exception_data d;
1663 id _rethrow = null;
1664
1665 objc_exception_try_enter(&d);
1666 if (!setjmp(d.jmp_buf)) {
1667 ... try body ...
1668 } else {
1669 // exception path
1670 id _caught = objc_exception_extract(&d);
1671
1672 // enter new try scope for handlers
1673 if (!setjmp(d.jmp_buf)) {
1674 ... match exception and execute catch blocks ...
1675
1676 // fell off end, rethrow.
1677 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001678 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001679 } else {
1680 // exception in catch block
1681 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001682 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001683 }
1684 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001685 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001686
1687finally:
1688 // match either the initial try_enter or the catch try_enter,
1689 // depending on the path followed.
1690 objc_exception_try_exit(&d);
1691finally_no_exit:
1692 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001693 ... dispatch to finally destination ...
1694
1695finally_rethrow:
1696 objc_exception_throw(_rethrow);
1697
1698finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001699}
1700
1701This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001702uses _rethrow to determine if objc_exception_try_exit should be called
1703and if the object should be rethrown. This breaks in the face of
1704throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001705
1706We specialize this framework for a few particular circumstances:
1707
1708 - If there are no catch blocks, then we avoid emitting the second
1709 exception handling context.
1710
1711 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1712 e)) we avoid emitting the code to rethrow an uncaught exception.
1713
1714 - FIXME: If there is no @finally block we can do a few more
1715 simplifications.
1716
1717Rethrows and Jumps-Through-Finally
1718--
1719
1720Support for implicit rethrows and jumping through the finally block is
1721handled by storing the current exception-handling context in
1722ObjCEHStack.
1723
Daniel Dunbar898d5082008-09-30 01:06:03 +00001724In order to implement proper @finally semantics, we support one basic
1725mechanism for jumping through the finally block to an arbitrary
1726destination. Constructs which generate exits from a @try or @catch
1727block use this mechanism to implement the proper semantics by chaining
1728jumps, as necessary.
1729
1730This mechanism works like the one used for indirect goto: we
1731arbitrarily assign an ID to each destination and store the ID for the
1732destination in a variable prior to entering the finally block. At the
1733end of the finally block we simply create a switch to the proper
1734destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001735
1736Code gen for @synchronized(expr) stmt;
1737Effectively generating code for:
1738objc_sync_enter(expr);
1739@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001740*/
1741
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001742void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1743 const Stmt &S) {
1744 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001745 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001746 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1747 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1748 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1749 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001750 llvm::Value *DestCode =
1751 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1752
1753 // Generate jump code. Done here so we can directly add things to
1754 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001755 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001756 llvm::SwitchInst *FinallySwitch =
1757 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1758 FinallyEnd, 10, FinallyJump);
1759
1760 // Push an EH context entry, used for handling rethrows and jumps
1761 // through finally.
1762 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1763 FinallySwitch, DestCode);
1764 CGF.ObjCEHStack.push_back(&EHEntry);
1765
1766 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001767 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1768 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001769 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1770 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001771 if (!isTry) {
1772 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001773 llvm::Value *Arg = CGF.EmitScalarExpr(
1774 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1775 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1776 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001777 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001778
1779 // Enter a new try block and call setjmp.
1780 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1781 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1782 "jmpbufarray");
1783 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1784 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1785 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001786
Daniel Dunbar55e87422008-11-11 02:29:29 +00001787 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1788 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001789 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001790 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001791
1792 // Emit the @try block.
1793 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001794 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1795 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001796 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001797
1798 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001799 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001800
1801 // Retrieve the exception object. We may emit multiple blocks but
1802 // nothing can cross this so the value is already in SSA form.
1803 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1804 ExceptionData,
1805 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001806 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001807 if (!isTry)
1808 {
1809 CGF.Builder.CreateStore(Caught, RethrowPtr);
1810 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1811 }
1812 else if (const ObjCAtCatchStmt* CatchStmt =
1813 cast<ObjCAtTryStmt>(S).getCatchStmts())
1814 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001815 // Enter a new exception try block (in case a @catch block throws
1816 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001817 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001818
Anders Carlsson80f25672008-09-09 17:59:25 +00001819 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1820 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001821 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001822
Daniel Dunbar55e87422008-11-11 02:29:29 +00001823 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1824 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001825 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001826
1827 CGF.EmitBlock(CatchBlock);
1828
Daniel Dunbar55e40722008-09-27 07:03:52 +00001829 // Handle catch list. As a special case we check if everything is
1830 // matched and avoid generating code for falling off the end if
1831 // so.
1832 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001833 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001834 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001835
Anders Carlssondde0a942008-09-11 09:15:33 +00001836 const DeclStmt *CatchParam =
1837 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001838 const VarDecl *VD = 0;
1839 const PointerType *PT = 0;
1840
Anders Carlsson80f25672008-09-09 17:59:25 +00001841 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001842 if (!CatchParam) {
1843 AllMatched = true;
1844 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001845 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001846 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001847
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001848 // catch(id e) always matches.
1849 // FIXME: For the time being we also match id<X>; this should
1850 // be rejected by Sema instead.
1851 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1852 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001853 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001854 }
1855
Daniel Dunbar55e40722008-09-27 07:03:52 +00001856 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001857 if (CatchParam) {
1858 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001859 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001860 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001861 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001862
Anders Carlssondde0a942008-09-11 09:15:33 +00001863 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001864 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001865 break;
1866 }
1867
Daniel Dunbar129271a2008-09-27 07:36:24 +00001868 assert(PT && "Unexpected non-pointer type in @catch");
1869 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001870 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001871 assert(ObjCType && "Catch parameter must have Objective-C type!");
1872
1873 // Check if the @catch block matches the exception object.
1874 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1875
Anders Carlsson80f25672008-09-09 17:59:25 +00001876 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1877 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001878
Daniel Dunbar55e87422008-11-11 02:29:29 +00001879 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001880
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001881 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001882 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001883
1884 // Emit the @catch block.
1885 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001886 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001887 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001888
1889 llvm::Value *Tmp =
1890 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1891 "tmp");
1892 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001893
1894 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001895 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001896
1897 CGF.EmitBlock(NextCatchBlock);
1898 }
1899
Daniel Dunbar55e40722008-09-27 07:03:52 +00001900 if (!AllMatched) {
1901 // None of the handlers caught the exception, so store it to be
1902 // rethrown at the end of the @finally block.
1903 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001904 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001905 }
1906
1907 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001908 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001909 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1910 ExceptionData),
1911 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001912 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001913 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001914 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001915 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001916 }
1917
Daniel Dunbar898d5082008-09-30 01:06:03 +00001918 // Pop the exception-handling stack entry. It is important to do
1919 // this now, because the code in the @finally block is not in this
1920 // context.
1921 CGF.ObjCEHStack.pop_back();
1922
Anders Carlsson80f25672008-09-09 17:59:25 +00001923 // Emit the @finally block.
1924 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001925 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001926
1927 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001928 if (isTry) {
1929 if (const ObjCAtFinallyStmt* FinallyStmt =
1930 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1931 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1932 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001933 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001934 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001935 // For @synchronized, call objc_sync_enter(sync.expr)
1936 llvm::Value *Arg = CGF.EmitScalarExpr(
1937 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1938 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1939 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1940 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001941
Daniel Dunbar898d5082008-09-30 01:06:03 +00001942 CGF.EmitBlock(FinallyJump);
1943
1944 CGF.EmitBlock(FinallyRethrow);
1945 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1946 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001947 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001948
1949 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001950}
1951
1952void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001953 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001954 llvm::Value *ExceptionAsObject;
1955
1956 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1957 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1958 ExceptionAsObject =
1959 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1960 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001961 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001962 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001963 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001964 }
1965
1966 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001967 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001968
1969 // Clear the insertion point to indicate we are in unreachable code.
1970 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001971}
1972
Daniel Dunbar898d5082008-09-30 01:06:03 +00001973void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1974 llvm::BasicBlock *Dst,
1975 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001976 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001977 return;
1978
1979 // Find the destination code for this block. We always use 0 for the
1980 // fallthrough block (default destination).
1981 llvm::SwitchInst *SI = E->FinallySwitch;
1982 llvm::ConstantInt *ID;
1983 if (Dst == SI->getDefaultDest()) {
1984 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1985 } else {
1986 ID = SI->findCaseDest(Dst);
1987 if (!ID) {
1988 // No code found, get a new unique one by just using the number
1989 // of switch successors.
1990 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1991 SI->addCase(ID, Dst);
1992 }
1993 }
1994
1995 // Set the destination code and branch.
1996 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001997 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001998}
1999
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002000/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002001/// object: objc_read_weak (id *src)
2002///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002003llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002004 llvm::Value *AddrWeakObj)
2005{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002006 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002007 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002008 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002009 return read_weak;
2010}
2011
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002012/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2013/// objc_assign_weak (id src, id *dst)
2014///
2015void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2016 llvm::Value *src, llvm::Value *dst)
2017{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002018 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2019 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002020 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2021 src, dst, "weakassign");
2022 return;
2023}
2024
Fariborz Jahanian58626502008-11-19 00:59:10 +00002025/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2026/// objc_assign_global (id src, id *dst)
2027///
2028void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2029 llvm::Value *src, llvm::Value *dst)
2030{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002031 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2032 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002033 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2034 src, dst, "globalassign");
2035 return;
2036}
2037
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002038/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2039/// objc_assign_ivar (id src, id *dst)
2040///
2041void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2042 llvm::Value *src, llvm::Value *dst)
2043{
2044 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2045 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2046 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2047 src, dst, "assignivar");
2048 return;
2049}
2050
Fariborz Jahanian58626502008-11-19 00:59:10 +00002051/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2052/// objc_assign_strongCast (id src, id *dst)
2053///
2054void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2055 llvm::Value *src, llvm::Value *dst)
2056{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002057 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2058 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002059 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2060 src, dst, "weakassign");
2061 return;
2062}
2063
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002064/* *** Private Interface *** */
2065
2066/// EmitImageInfo - Emit the image info marker used to encode some module
2067/// level information.
2068///
2069/// See: <rdr://4810609&4810587&4810587>
2070/// struct IMAGE_INFO {
2071/// unsigned version;
2072/// unsigned flags;
2073/// };
2074enum ImageInfoFlags {
2075 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2076 eImageInfo_GarbageCollected = (1 << 1),
2077 eImageInfo_GCOnly = (1 << 2)
2078};
2079
2080void CGObjCMac::EmitImageInfo() {
2081 unsigned version = 0; // Version is unused?
2082 unsigned flags = 0;
2083
2084 // FIXME: Fix and continue?
2085 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2086 flags |= eImageInfo_GarbageCollected;
2087 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2088 flags |= eImageInfo_GCOnly;
2089
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002090 // Emitted as int[2];
2091 llvm::Constant *values[2] = {
2092 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2093 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2094 };
2095 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002096 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002097 new llvm::GlobalVariable(AT, true,
2098 llvm::GlobalValue::InternalLinkage,
2099 llvm::ConstantArray::get(AT, values, 2),
2100 "\01L_OBJC_IMAGE_INFO",
2101 &CGM.getModule());
2102
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002103 if (ObjCABI == 1) {
2104 GV->setSection("__OBJC, __image_info,regular");
2105 } else {
2106 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2107 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002108
2109 UsedGlobals.push_back(GV);
2110}
2111
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002112
2113// struct objc_module {
2114// unsigned long version;
2115// unsigned long size;
2116// const char *name;
2117// Symtab symtab;
2118// };
2119
2120// FIXME: Get from somewhere
2121static const int ModuleVersion = 7;
2122
2123void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002124 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002125
2126 std::vector<llvm::Constant*> Values(4);
2127 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2128 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002129 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002130 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002131 Values[3] = EmitModuleSymbols();
2132
2133 llvm::GlobalVariable *GV =
2134 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2135 llvm::GlobalValue::InternalLinkage,
2136 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2137 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002138 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002139 &CGM.getModule());
2140 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2141 UsedGlobals.push_back(GV);
2142}
2143
2144llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002145 unsigned NumClasses = DefinedClasses.size();
2146 unsigned NumCategories = DefinedCategories.size();
2147
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002148 // Return null if no symbols were defined.
2149 if (!NumClasses && !NumCategories)
2150 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2151
2152 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2154 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2155 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2156 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2157
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002158 // The runtime expects exactly the list of defined classes followed
2159 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002160 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002161 for (unsigned i=0; i<NumClasses; i++)
2162 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2163 ObjCTypes.Int8PtrTy);
2164 for (unsigned i=0; i<NumCategories; i++)
2165 Symbols[NumClasses + i] =
2166 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2167 ObjCTypes.Int8PtrTy);
2168
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002169 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002170 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002171 NumClasses + NumCategories),
2172 Symbols);
2173
2174 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2175
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002176 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002178 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002180 "\01L_OBJC_SYMBOLS",
2181 &CGM.getModule());
2182 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2183 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2185}
2186
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002187llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002188 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002189 LazySymbols.insert(ID->getIdentifier());
2190
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002191 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2192
2193 if (!Entry) {
2194 llvm::Constant *Casted =
2195 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2196 ObjCTypes.ClassPtrTy);
2197 Entry =
2198 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2199 llvm::GlobalValue::InternalLinkage,
2200 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2201 &CGM.getModule());
2202 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2203 UsedGlobals.push_back(Entry);
2204 }
2205
2206 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002207}
2208
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002209llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002210 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2211
2212 if (!Entry) {
2213 llvm::Constant *Casted =
2214 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2215 ObjCTypes.SelectorPtrTy);
2216 Entry =
2217 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2218 llvm::GlobalValue::InternalLinkage,
2219 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2220 &CGM.getModule());
2221 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2222 UsedGlobals.push_back(Entry);
2223 }
2224
2225 return Builder.CreateLoad(Entry, false, "tmp");
2226}
2227
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002228llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002229 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002230
2231 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002232 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002233 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002234 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002235 llvm::GlobalValue::InternalLinkage,
2236 C, "\01L_OBJC_CLASS_NAME_",
2237 &CGM.getModule());
2238 Entry->setSection("__TEXT,__cstring,cstring_literals");
2239 UsedGlobals.push_back(Entry);
2240 }
2241
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002242 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002243}
2244
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002245llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002246 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2247
2248 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002249 // FIXME: Avoid std::string copying.
2250 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002251 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002252 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002253 llvm::GlobalValue::InternalLinkage,
2254 C, "\01L_OBJC_METH_VAR_NAME_",
2255 &CGM.getModule());
2256 Entry->setSection("__TEXT,__cstring,cstring_literals");
2257 UsedGlobals.push_back(Entry);
2258 }
2259
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002260 return getConstantGEP(Entry, 0, 0);
2261}
2262
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002264llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002265 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2266}
2267
2268// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002269llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2271}
2272
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002273llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002275
2276 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002278 Entry =
2279 new llvm::GlobalVariable(C->getType(), false,
2280 llvm::GlobalValue::InternalLinkage,
2281 C, "\01L_OBJC_METH_VAR_TYPE_",
2282 &CGM.getModule());
2283 Entry->setSection("__TEXT,__cstring,cstring_literals");
2284 UsedGlobals.push_back(Entry);
2285 }
2286
2287 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002288}
2289
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002291llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002292 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002293 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2294 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002295 return GetMethodVarType(TypeStr);
2296}
2297
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002298// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002299llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002300 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2301
2302 if (!Entry) {
2303 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2304 Entry =
2305 new llvm::GlobalVariable(C->getType(), false,
2306 llvm::GlobalValue::InternalLinkage,
2307 C, "\01L_OBJC_PROP_NAME_ATTR_",
2308 &CGM.getModule());
2309 Entry->setSection("__TEXT,__cstring,cstring_literals");
2310 UsedGlobals.push_back(Entry);
2311 }
2312
2313 return getConstantGEP(Entry, 0, 0);
2314}
2315
2316// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002317// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002318llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002319 const Decl *Container) {
2320 std::string TypeStr;
2321 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002322 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2323}
2324
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002325void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2326 const ObjCContainerDecl *CD,
2327 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002328 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002329 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002330 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002331 assert (CD && "Missing container decl in GetNameForMethod");
2332 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002333 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2334 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002335 NameOut += ' ';
2336 NameOut += D->getSelector().getAsString();
2337 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002338}
2339
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002340void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002341 EmitModuleInfo();
2342
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002343 // Emit the dummy bodies for any protocols which were referenced but
2344 // never defined.
2345 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2346 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2347 if (i->second->hasInitializer())
2348 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002349
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002350 std::vector<llvm::Constant*> Values(5);
2351 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2352 Values[1] = GetClassName(i->first);
2353 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2354 Values[3] = Values[4] =
2355 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2356 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2357 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2358 Values));
2359 }
2360
2361 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002362 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002363 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002364 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002365 }
2366
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002367 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002368 llvm::GlobalValue *GV =
2369 new llvm::GlobalVariable(AT, false,
2370 llvm::GlobalValue::AppendingLinkage,
2371 llvm::ConstantArray::get(AT, Used),
2372 "llvm.used",
2373 &CGM.getModule());
2374
2375 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002376
2377 // Add assembler directives to add lazy undefined symbol references
2378 // for classes which are referenced but not defined. This is
2379 // important for correct linker interaction.
2380
2381 // FIXME: Uh, this isn't particularly portable.
2382 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002383
2384 if (!CGM.getModule().getModuleInlineAsm().empty())
2385 s << "\n";
2386
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002387 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2388 e = LazySymbols.end(); i != e; ++i) {
2389 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2390 }
2391 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2392 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002393 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002394 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2395 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002396
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002397 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002398}
2399
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002400CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002401 : CGObjCCommonMac(cgm),
2402 ObjCTypes(cgm)
2403{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002404 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002405 ObjCABI = 2;
2406}
2407
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002408/* *** */
2409
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002410ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2411: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002412{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002413 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2414 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002415
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002417 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002418 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002419 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2420
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002421 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002422 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002423 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002424
2425 // FIXME: It would be nice to unify this with the opaque type, so
2426 // that the IR comes out a bit cleaner.
2427 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2428 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002429
2430 // I'm not sure I like this. The implicit coordination is a bit
2431 // gross. We should solve this in a reasonable fashion because this
2432 // is a pretty common task (match some runtime data structure with
2433 // an LLVM data structure).
2434
2435 // FIXME: This is leaked.
2436 // FIXME: Merge with rewriter code?
2437
2438 // struct _objc_super {
2439 // id self;
2440 // Class cls;
2441 // }
2442 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2443 SourceLocation(),
2444 &Ctx.Idents.get("_objc_super"));
2445 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2446 Ctx.getObjCIdType(), 0, false));
2447 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2448 Ctx.getObjCClassType(), 0, false));
2449 RD->completeDefinition(Ctx);
2450
2451 SuperCTy = Ctx.getTagDeclType(RD);
2452 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2453
2454 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002455 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2456
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002457 // struct _prop_t {
2458 // char *name;
2459 // char *attributes;
2460 // }
2461 PropertyTy = llvm::StructType::get(Int8PtrTy,
2462 Int8PtrTy,
2463 NULL);
2464 CGM.getModule().addTypeName("struct._prop_t",
2465 PropertyTy);
2466
2467 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002468 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002469 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002470 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002471 // }
2472 PropertyListTy = llvm::StructType::get(IntTy,
2473 IntTy,
2474 llvm::ArrayType::get(PropertyTy, 0),
2475 NULL);
2476 CGM.getModule().addTypeName("struct._prop_list_t",
2477 PropertyListTy);
2478 // struct _prop_list_t *
2479 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2480
2481 // struct _objc_method {
2482 // SEL _cmd;
2483 // char *method_type;
2484 // char *_imp;
2485 // }
2486 MethodTy = llvm::StructType::get(SelectorPtrTy,
2487 Int8PtrTy,
2488 Int8PtrTy,
2489 NULL);
2490 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002491
2492 // struct _objc_cache *
2493 CacheTy = llvm::OpaqueType::get();
2494 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2495 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002496
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002497 // Property manipulation functions.
2498
2499 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2500 std::vector<const llvm::Type*> Params;
2501 Params.push_back(ObjectPtrTy);
2502 Params.push_back(SelectorPtrTy);
2503 Params.push_back(LongTy);
2504 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2505 GetPropertyFn =
2506 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2507 Params,
2508 false),
2509 "objc_getProperty");
2510
2511 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2512 Params.clear();
2513 Params.push_back(ObjectPtrTy);
2514 Params.push_back(SelectorPtrTy);
2515 Params.push_back(LongTy);
2516 Params.push_back(ObjectPtrTy);
2517 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2518 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2519 SetPropertyFn =
2520 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2521 Params,
2522 false),
2523 "objc_setProperty");
2524 // Enumeration mutation.
2525
2526 Params.clear();
2527 Params.push_back(ObjectPtrTy);
2528 EnumerationMutationFn =
2529 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2530 Params,
2531 false),
2532 "objc_enumerationMutation");
2533
2534 // gc's API
2535 // id objc_read_weak (id *)
2536 Params.clear();
2537 Params.push_back(PtrObjectPtrTy);
2538 GcReadWeakFn =
2539 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2540 Params,
2541 false),
2542 "objc_read_weak");
2543 // id objc_assign_weak (id, id *)
2544 Params.clear();
2545 Params.push_back(ObjectPtrTy);
2546 Params.push_back(PtrObjectPtrTy);
2547 GcAssignWeakFn =
2548 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2549 Params,
2550 false),
2551 "objc_assign_weak");
2552 GcAssignGlobalFn =
2553 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2554 Params,
2555 false),
2556 "objc_assign_global");
2557 GcAssignIvarFn =
2558 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2559 Params,
2560 false),
2561 "objc_assign_ivar");
2562 GcAssignStrongCastFn =
2563 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2564 Params,
2565 false),
2566 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002567}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002568
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002569ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2570 : ObjCCommonTypesHelper(cgm)
2571{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002572 // struct _objc_method_description {
2573 // SEL name;
2574 // char *types;
2575 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002576 MethodDescriptionTy =
2577 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002578 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002579 NULL);
2580 CGM.getModule().addTypeName("struct._objc_method_description",
2581 MethodDescriptionTy);
2582
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002583 // struct _objc_method_description_list {
2584 // int count;
2585 // struct _objc_method_description[1];
2586 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002587 MethodDescriptionListTy =
2588 llvm::StructType::get(IntTy,
2589 llvm::ArrayType::get(MethodDescriptionTy, 0),
2590 NULL);
2591 CGM.getModule().addTypeName("struct._objc_method_description_list",
2592 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002593
2594 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002595 MethodDescriptionListPtrTy =
2596 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2597
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002598 // Protocol description structures
2599
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002600 // struct _objc_protocol_extension {
2601 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2602 // struct _objc_method_description_list *optional_instance_methods;
2603 // struct _objc_method_description_list *optional_class_methods;
2604 // struct _objc_property_list *instance_properties;
2605 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002606 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002607 llvm::StructType::get(IntTy,
2608 MethodDescriptionListPtrTy,
2609 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002610 PropertyListPtrTy,
2611 NULL);
2612 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2613 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002614
2615 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002616 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2617
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002618 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002619
2620 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2621 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2622
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002623 const llvm::Type *T =
2624 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2625 LongTy,
2626 llvm::ArrayType::get(ProtocolTyHolder, 0),
2627 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002628 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2629
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002630 // struct _objc_protocol {
2631 // struct _objc_protocol_extension *isa;
2632 // char *protocol_name;
2633 // struct _objc_protocol **_objc_protocol_list;
2634 // struct _objc_method_description_list *instance_methods;
2635 // struct _objc_method_description_list *class_methods;
2636 // }
2637 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002638 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002639 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2640 MethodDescriptionListPtrTy,
2641 MethodDescriptionListPtrTy,
2642 NULL);
2643 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2644
2645 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2646 CGM.getModule().addTypeName("struct._objc_protocol_list",
2647 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002648 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002649 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2650
2651 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002652 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002653 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002654
2655 // Class description structures
2656
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002657 // struct _objc_ivar {
2658 // char *ivar_name;
2659 // char *ivar_type;
2660 // int ivar_offset;
2661 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002662 IvarTy = llvm::StructType::get(Int8PtrTy,
2663 Int8PtrTy,
2664 IntTy,
2665 NULL);
2666 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2667
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002668 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002669 IvarListTy = llvm::OpaqueType::get();
2670 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2671 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2672
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002673 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002674 MethodListTy = llvm::OpaqueType::get();
2675 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2676 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2677
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002678 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002679 ClassExtensionTy =
2680 llvm::StructType::get(IntTy,
2681 Int8PtrTy,
2682 PropertyListPtrTy,
2683 NULL);
2684 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2685 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2686
2687 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2688
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002689 // struct _objc_class {
2690 // Class isa;
2691 // Class super_class;
2692 // char *name;
2693 // long version;
2694 // long info;
2695 // long instance_size;
2696 // struct _objc_ivar_list *ivars;
2697 // struct _objc_method_list *methods;
2698 // struct _objc_cache *cache;
2699 // struct _objc_protocol_list *protocols;
2700 // char *ivar_layout;
2701 // struct _objc_class_ext *ext;
2702 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002703 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2704 llvm::PointerType::getUnqual(ClassTyHolder),
2705 Int8PtrTy,
2706 LongTy,
2707 LongTy,
2708 LongTy,
2709 IvarListPtrTy,
2710 MethodListPtrTy,
2711 CachePtrTy,
2712 ProtocolListPtrTy,
2713 Int8PtrTy,
2714 ClassExtensionPtrTy,
2715 NULL);
2716 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2717
2718 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2719 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2720 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2721
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002722 // struct _objc_category {
2723 // char *category_name;
2724 // char *class_name;
2725 // struct _objc_method_list *instance_method;
2726 // struct _objc_method_list *class_method;
2727 // uint32_t size; // sizeof(struct _objc_category)
2728 // struct _objc_property_list *instance_properties;// category's @property
2729 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002730 CategoryTy = llvm::StructType::get(Int8PtrTy,
2731 Int8PtrTy,
2732 MethodListPtrTy,
2733 MethodListPtrTy,
2734 ProtocolListPtrTy,
2735 IntTy,
2736 PropertyListPtrTy,
2737 NULL);
2738 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2739
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002740 // Global metadata structures
2741
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002742 // struct _objc_symtab {
2743 // long sel_ref_cnt;
2744 // SEL *refs;
2745 // short cls_def_cnt;
2746 // short cat_def_cnt;
2747 // char *defs[cls_def_cnt + cat_def_cnt];
2748 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002749 SymtabTy = llvm::StructType::get(LongTy,
2750 SelectorPtrTy,
2751 ShortTy,
2752 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002753 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002754 NULL);
2755 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2756 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2757
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002758 // struct _objc_module {
2759 // long version;
2760 // long size; // sizeof(struct _objc_module)
2761 // char *name;
2762 // struct _objc_symtab* symtab;
2763 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002764 ModuleTy =
2765 llvm::StructType::get(LongTy,
2766 LongTy,
2767 Int8PtrTy,
2768 SymtabPtrTy,
2769 NULL);
2770 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002771
Daniel Dunbar49f66022008-09-24 03:38:44 +00002772 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002773
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002774 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002775 std::vector<const llvm::Type*> Params;
2776 Params.push_back(ObjectPtrTy);
2777 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002778 MessageSendFn =
2779 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2780 Params,
2781 true),
2782 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002783
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002784 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002785 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002786 Params.push_back(ObjectPtrTy);
2787 Params.push_back(SelectorPtrTy);
2788 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002789 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2790 Params,
2791 true),
2792 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002793
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002794 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002795 Params.clear();
2796 Params.push_back(ObjectPtrTy);
2797 Params.push_back(SelectorPtrTy);
2798 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002799 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002800 MessageSendFpretFn =
2801 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2802 Params,
2803 true),
2804 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002805
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002806 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002807 Params.clear();
2808 Params.push_back(SuperPtrTy);
2809 Params.push_back(SelectorPtrTy);
2810 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002811 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2812 Params,
2813 true),
2814 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002815
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002816 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2817 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002818 Params.clear();
2819 Params.push_back(Int8PtrTy);
2820 Params.push_back(SuperPtrTy);
2821 Params.push_back(SelectorPtrTy);
2822 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002823 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2824 Params,
2825 true),
2826 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002827
2828 // There is no objc_msgSendSuper_fpret? How can that work?
2829 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002830
Anders Carlsson124526b2008-09-09 10:10:21 +00002831 // FIXME: This is the size of the setjmp buffer and should be
2832 // target specific. 18 is what's used on 32-bit X86.
2833 uint64_t SetJmpBufferSize = 18;
2834
2835 // Exceptions
2836 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002837 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002838
2839 ExceptionDataTy =
2840 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2841 SetJmpBufferSize),
2842 StackPtrTy, NULL);
2843 CGM.getModule().addTypeName("struct._objc_exception_data",
2844 ExceptionDataTy);
2845
2846 Params.clear();
2847 Params.push_back(ObjectPtrTy);
2848 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002849 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2850 Params,
2851 false),
2852 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002853
2854 Params.clear();
2855 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2856 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002857 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2858 Params,
2859 false),
2860 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002861 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002862 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2863 Params,
2864 false),
2865 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002866 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002867 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2868 Params,
2869 false),
2870 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002871
2872 Params.clear();
2873 Params.push_back(ClassPtrTy);
2874 Params.push_back(ObjectPtrTy);
2875 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002876 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2877 Params,
2878 false),
2879 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002880
2881 // synchronized APIs
2882 // void objc_sync_enter (id)
2883 Params.clear();
2884 Params.push_back(ObjectPtrTy);
2885 SyncEnterFn =
2886 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2887 Params,
2888 false),
2889 "objc_sync_enter");
2890 // void objc_sync_exit (id)
2891 SyncExitFn =
2892 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2893 Params,
2894 false),
2895 "objc_sync_exit");
2896
Anders Carlsson124526b2008-09-09 10:10:21 +00002897
2898 Params.clear();
2899 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2900 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002901 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2902 Params,
2903 false),
2904 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002905
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002906}
2907
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002908ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002909: ObjCCommonTypesHelper(cgm)
2910{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002911 // struct _method_list_t {
2912 // uint32_t entsize; // sizeof(struct _objc_method)
2913 // uint32_t method_count;
2914 // struct _objc_method method_list[method_count];
2915 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002916 MethodListnfABITy = llvm::StructType::get(IntTy,
2917 IntTy,
2918 llvm::ArrayType::get(MethodTy, 0),
2919 NULL);
2920 CGM.getModule().addTypeName("struct.__method_list_t",
2921 MethodListnfABITy);
2922 // struct method_list_t *
2923 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002924
2925 // struct _protocol_t {
2926 // id isa; // NULL
2927 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002928 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002929 // const struct method_list_t * const instance_methods;
2930 // const struct method_list_t * const class_methods;
2931 // const struct method_list_t *optionalInstanceMethods;
2932 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002933 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002934 // const uint32_t size; // sizeof(struct _protocol_t)
2935 // const uint32_t flags; // = 0
2936 // }
2937
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002938 // Holder for struct _protocol_list_t *
2939 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2940
2941 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2942 Int8PtrTy,
2943 llvm::PointerType::getUnqual(
2944 ProtocolListTyHolder),
2945 MethodListnfABIPtrTy,
2946 MethodListnfABIPtrTy,
2947 MethodListnfABIPtrTy,
2948 MethodListnfABIPtrTy,
2949 PropertyListPtrTy,
2950 IntTy,
2951 IntTy,
2952 NULL);
2953 CGM.getModule().addTypeName("struct._protocol_t",
2954 ProtocolnfABITy);
2955
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002956 // struct _objc_protocol_list {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002957 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002958 // struct _protocol_t[protocol_count];
2959 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002960 ProtocolListnfABITy = llvm::StructType::get(LongTy,
2961 llvm::ArrayType::get(
2962 ProtocolnfABITy, 0),
2963 NULL);
2964 CGM.getModule().addTypeName("struct._objc_protocol_list",
2965 ProtocolListnfABITy);
2966
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002967 // struct _objc_protocol_list*
2968 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002969
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002970 // FIXME! Is this doing the right thing?
2971 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
2972 ProtocolListnfABIPtrTy);
2973
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002974 // struct _ivar_t {
2975 // unsigned long int *offset; // pointer to ivar offset location
2976 // char *name;
2977 // char *type;
2978 // uint32_t alignment;
2979 // uint32_t size;
2980 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002981 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
2982 Int8PtrTy,
2983 Int8PtrTy,
2984 IntTy,
2985 IntTy,
2986 NULL);
2987 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
2988
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002989 // struct _ivar_list_t {
2990 // uint32 entsize; // sizeof(struct _ivar_t)
2991 // uint32 count;
2992 // struct _iver_t list[count];
2993 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002994 IvarListnfABITy = llvm::StructType::get(IntTy,
2995 IntTy,
2996 llvm::ArrayType::get(
2997 IvarnfABITy, 0),
2998 NULL);
2999 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3000
3001 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003002
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003003 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003004 // uint32_t const flags;
3005 // uint32_t const instanceStart;
3006 // uint32_t const instanceSize;
3007 // uint32_t const reserved; // only when building for 64bit targets
3008 // const uint8_t * const ivarLayout;
3009 // const char *const name;
3010 // const struct _method_list_t * const baseMethods;
3011 // const struct _objc_protocol_list *const baseProtocols;
3012 // const struct _ivar_list_t *const ivars;
3013 // const uint8_t * const weakIvarLayout;
3014 // const struct _prop_list_t * const properties;
3015 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003016
3017 // FIXME. Add 'reserved' field in 64bit abi mode!
3018 ClassRonfABITy = llvm::StructType::get(IntTy,
3019 IntTy,
3020 IntTy,
3021 Int8PtrTy,
3022 Int8PtrTy,
3023 MethodListnfABIPtrTy,
3024 ProtocolListnfABIPtrTy,
3025 IvarListnfABIPtrTy,
3026 Int8PtrTy,
3027 PropertyListPtrTy,
3028 NULL);
3029 CGM.getModule().addTypeName("struct._class_ro_t",
3030 ClassRonfABITy);
3031
3032 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3033 std::vector<const llvm::Type*> Params;
3034 Params.push_back(ObjectPtrTy);
3035 Params.push_back(SelectorPtrTy);
3036 ImpnfABITy = llvm::PointerType::getUnqual(
3037 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3038
3039 // struct _class_t {
3040 // struct _class_t *isa;
3041 // struct _class_t * const superclass;
3042 // void *cache;
3043 // IMP *vtable;
3044 // struct class_ro_t *ro;
3045 // }
3046
3047 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3048 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3049 llvm::PointerType::getUnqual(ClassTyHolder),
3050 CachePtrTy,
3051 llvm::PointerType::getUnqual(ImpnfABITy),
3052 llvm::PointerType::getUnqual(
3053 ClassRonfABITy),
3054 NULL);
3055 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3056
3057 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3058 ClassnfABITy);
3059
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003060 // LLVM for struct _class_t *
3061 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3062
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003063 // struct _category_t {
3064 // const char * const name;
3065 // struct _class_t *const cls;
3066 // const struct _method_list_t * const instance_methods;
3067 // const struct _method_list_t * const class_methods;
3068 // const struct _protocol_list_t * const protocols;
3069 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003070 // }
3071 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003072 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003073 MethodListnfABIPtrTy,
3074 MethodListnfABIPtrTy,
3075 ProtocolListnfABIPtrTy,
3076 PropertyListPtrTy,
3077 NULL);
3078 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3079
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003080}
3081
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003082llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3083 FinishNonFragileABIModule();
3084
3085 return NULL;
3086}
3087
3088void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3089 // nonfragile abi has no module definition.
3090 std::vector<llvm::Constant*> Used;
3091 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3092 e = UsedGlobals.end(); i != e; ++i) {
3093 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3094 }
3095
3096 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3097 llvm::GlobalValue *GV =
3098 new llvm::GlobalVariable(AT, false,
3099 llvm::GlobalValue::AppendingLinkage,
3100 llvm::ConstantArray::get(AT, Used),
3101 "llvm.used",
3102 &CGM.getModule());
3103
3104 GV->setSection("llvm.metadata");
3105
3106}
3107
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003108// Metadata flags
3109enum MetaDataDlags {
3110 CLS = 0x0,
3111 CLS_META = 0x1,
3112 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003113 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003114 CLS_EXCEPTION = 0x20
3115};
3116/// BuildClassRoTInitializer - generate meta-data for:
3117/// struct _class_ro_t {
3118/// uint32_t const flags;
3119/// uint32_t const instanceStart;
3120/// uint32_t const instanceSize;
3121/// uint32_t const reserved; // only when building for 64bit targets
3122/// const uint8_t * const ivarLayout;
3123/// const char *const name;
3124/// const struct _method_list_t * const baseMethods;
3125/// const struct _objc_protocol_list *const baseProtocols;
3126/// const struct _ivar_list_t *const ivars;
3127/// const uint8_t * const weakIvarLayout;
3128/// const struct _prop_list_t * const properties;
3129/// }
3130///
3131llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3132 unsigned flags,
3133 unsigned InstanceStart,
3134 unsigned InstanceSize,
3135 const ObjCImplementationDecl *ID) {
3136 std::string ClassName = ID->getNameAsString();
3137 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3138 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3139 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3140 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3141 // FIXME. For 64bit targets add 0 here.
3142 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3143 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003144 // const struct _method_list_t * const baseMethods;
3145 std::vector<llvm::Constant*> Methods;
3146 std::string MethodListName("\01l_OBJC_$_");
3147 if (flags & CLS_META) {
3148 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3149 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3150 e = ID->classmeth_end(); i != e; ++i) {
3151 // Class methods should always be defined.
3152 Methods.push_back(GetMethodConstant(*i));
3153 }
3154 } else {
3155 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3156 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3157 e = ID->instmeth_end(); i != e; ++i) {
3158 // Instance methods should always be defined.
3159 Methods.push_back(GetMethodConstant(*i));
3160 }
3161 }
3162 // FIXME. Section may always be .data
3163 Values[ 5] = EmitMethodList(MethodListName,
3164 ".section __DATA,__data,regular,no_dead_strip", Methods);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003165 Values[ 6] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003166 if (flags & CLS_META)
3167 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3168 else
3169 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003170 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3171 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3172 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3173 Values);
3174 llvm::GlobalVariable *CLASS_RO_GV =
3175 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3176 llvm::GlobalValue::InternalLinkage,
3177 Init,
3178 (flags & CLS_META) ?
3179 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3180 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3181 &CGM.getModule());
3182 CLASS_RO_GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3183 UsedGlobals.push_back(CLASS_RO_GV);
3184 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003185
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003186}
3187
3188/// BuildClassMetaData - This routine defines that to-level meta-data
3189/// for the given ClassName for:
3190/// struct _class_t {
3191/// struct _class_t *isa;
3192/// struct _class_t * const superclass;
3193/// void *cache;
3194/// IMP *vtable;
3195/// struct class_ro_t *ro;
3196/// }
3197///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003198llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3199 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003200 llvm::Constant *IsAGV,
3201 llvm::Constant *SuperClassGV,
3202 llvm::Constant *ClassRoGV) {
3203 std::vector<llvm::Constant*> Values(5);
3204 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003205 Values[1] = SuperClassGV
3206 ? SuperClassGV
3207 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003208 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3209 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3210 Values[4] = ClassRoGV; // &CLASS_RO_GV
3211 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3212 Values);
3213 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3214 if (GV)
3215 GV->setInitializer(Init);
3216 else
3217 GV =
3218 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3219 llvm::GlobalValue::ExternalLinkage,
3220 Init,
3221 ClassName,
3222 &CGM.getModule());
3223 GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3224 UsedGlobals.push_back(GV);
3225 // FIXME! why?
3226 GV->setAlignment(32);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003227 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003228}
3229
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003230void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3231 std::string ClassName = ID->getNameAsString();
3232 if (!ObjCEmptyCacheVar) {
3233 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3234 ObjCTypes.CachePtrTy,
3235 false,
3236 llvm::GlobalValue::ExternalLinkage,
3237 0,
3238 "\01_objc_empty_cache",
3239 &CGM.getModule());
3240 UsedGlobals.push_back(ObjCEmptyCacheVar);
3241
3242 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3243 llvm::PointerType::getUnqual(
3244 ObjCTypes.ImpnfABITy),
3245 false,
3246 llvm::GlobalValue::ExternalLinkage,
3247 0,
3248 "\01_objc_empty_vtable",
3249 &CGM.getModule());
3250 UsedGlobals.push_back(ObjCEmptyVtableVar);
3251 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003252 assert(ID->getClassInterface() &&
3253 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003254 uint32_t InstanceStart =
3255 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3256 uint32_t InstanceSize = InstanceStart;
3257 uint32_t flags = CLS_META;
3258 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3259 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003260
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003261 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003262
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003263 if (IsClassHidden(ID->getClassInterface()))
3264 flags |= OBJC2_CLS_HIDDEN;
3265 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003266 // class is root
3267 flags |= CLS_ROOT;
3268 std::string SuperClassName = ObjCClassName + ClassName;
3269 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3270 if (!SuperClassGV)
3271 SuperClassGV =
3272 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3273 llvm::GlobalValue::ExternalLinkage,
3274 0,
3275 SuperClassName,
3276 &CGM.getModule());
3277 UsedGlobals.push_back(SuperClassGV);
3278 std::string IsAClassName = ObjCMetaClassName + ClassName;
3279 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3280 if (!IsAGV)
3281 IsAGV =
3282 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3283 llvm::GlobalValue::ExternalLinkage,
3284 0,
3285 IsAClassName,
3286 &CGM.getModule());
3287 UsedGlobals.push_back(IsAGV);
3288 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003289 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003290 std::string RootClassName =
3291 ID->getClassInterface()->getSuperClass()->getNameAsString();
3292 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3293 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3294 if (!SuperClassGV)
3295 SuperClassGV =
3296 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3297 llvm::GlobalValue::ExternalLinkage,
3298 0,
3299 SuperClassName,
3300 &CGM.getModule());
3301 UsedGlobals.push_back(SuperClassGV);
3302 IsAGV = SuperClassGV;
3303 }
3304 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3305 InstanceStart,
3306 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003307 std::string TClassName = ObjCMetaClassName + ClassName;
3308 llvm::GlobalVariable *MetaTClass =
3309 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3310
3311 // Metadata for the class
3312 flags = CLS;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003313 if (IsClassHidden(ID->getClassInterface()))
3314 flags |= OBJC2_CLS_HIDDEN;
3315 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003316 flags |= CLS_ROOT;
3317 SuperClassGV = 0;
3318 }
3319 else {
3320 // Has a root. Current class is not a root.
3321 std::string RootClassName =
3322 ID->getClassInterface()->getSuperClass()->getNameAsString();
3323 std::string SuperClassName = ObjCClassName + RootClassName;
3324 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3325 if (!SuperClassGV)
3326 SuperClassGV =
3327 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3328 llvm::GlobalValue::ExternalLinkage,
3329 0,
3330 SuperClassName,
3331 &CGM.getModule());
3332 UsedGlobals.push_back(SuperClassGV);
3333
3334 }
3335
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003336 InstanceStart = InstanceSize = 0;
3337 if (ObjCInterfaceDecl *OID =
3338 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3339 // FIXME. Share this with the one in EmitIvarList.
3340 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
3341 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
3342 RecordDecl::field_iterator firstField = RD->field_begin();
3343 RecordDecl::field_iterator lastField = RD->field_end();
3344
3345 while (countSuperClassIvars-- > 0) {
3346 lastField = firstField;
3347 ++firstField;
3348 }
3349
3350 for (RecordDecl::field_iterator e = RD->field_end(),
3351 ifield = firstField; ifield != e; ++ifield)
3352 lastField = ifield;
3353
3354 const llvm::Type *InterfaceTy =
3355 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3356 const llvm::StructLayout *Layout =
3357 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
3358
3359 if (lastField != RD->field_end()) {
3360 FieldDecl *Field = *lastField;
3361 const llvm::Type *FieldTy =
3362 CGM.getTypes().ConvertTypeForMem(Field->getType());
3363 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3364 InstanceSize = Layout->getElementOffset(
3365 CGM.getTypes().getLLVMFieldNo(Field)) +
3366 Size;
3367 if (firstField == RD->field_end())
3368 InstanceStart = InstanceSize;
3369 else
3370 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3371 getLLVMFieldNo(*firstField));
3372 }
3373 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003374 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003375 InstanceStart,
3376 InstanceSize,
3377 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003378
3379 TClassName = ObjCClassName + ClassName;
3380 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003381}
3382
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003383/// GenerateCategory - Build metadata for a category implementation.
3384/// struct _category_t {
3385/// const char * const name;
3386/// struct _class_t *const cls;
3387/// const struct _method_list_t * const instance_methods;
3388/// const struct _method_list_t * const class_methods;
3389/// const struct _protocol_list_t * const protocols;
3390/// const struct _prop_list_t * const properties;
3391/// }
3392///
3393void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3394{
3395 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003396 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3397 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003398 "_$_" + OCD->getNameAsString());
3399 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3400
3401 std::vector<llvm::Constant*> Values(6);
3402 Values[0] = GetClassName(OCD->getIdentifier());
3403 // meta-class entry symbol
3404 llvm::GlobalVariable *ClassGV =
3405 CGM.getModule().getGlobalVariable(ExtClassName);
3406 if (!ClassGV)
3407 ClassGV =
3408 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3409 llvm::GlobalValue::ExternalLinkage,
3410 0,
3411 ExtClassName,
3412 &CGM.getModule());
3413 UsedGlobals.push_back(ClassGV);
3414 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003415 std::vector<llvm::Constant*> Methods;
3416 std::string MethodListName(Prefix);
3417 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3418 "_$_" + OCD->getNameAsString();
3419
3420 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3421 e = OCD->instmeth_end(); i != e; ++i) {
3422 // Instance methods should always be defined.
3423 Methods.push_back(GetMethodConstant(*i));
3424 }
3425
3426 Values[2] = EmitMethodList(MethodListName,
3427 ".section __DATA,__data,regular,no_dead_strip",
3428 Methods);
3429
3430 MethodListName = Prefix;
3431 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3432 OCD->getNameAsString();
3433 Methods.clear();
3434 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3435 e = OCD->classmeth_end(); i != e; ++i) {
3436 // Class methods should always be defined.
3437 Methods.push_back(GetMethodConstant(*i));
3438 }
3439
3440 Values[3] = EmitMethodList(MethodListName,
3441 ".section __DATA,__data,regular,no_dead_strip",
3442 Methods);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003443 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3444 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3445
3446 llvm::Constant *Init =
3447 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3448 Values);
3449 llvm::GlobalVariable *GCATV
3450 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3451 false,
3452 llvm::GlobalValue::InternalLinkage,
3453 Init,
3454 ExtCatName,
3455 &CGM.getModule());
3456 GCATV->setSection(".section __DATA,__data,regular,no_dead_strip");
3457 UsedGlobals.push_back(GCATV);
3458 DefinedCategories.push_back(GCATV);
3459}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003460
3461/// GetMethodConstant - Return a struct objc_method constant for the
3462/// given method if it has been defined. The result is null if the
3463/// method has not been defined. The return value has type MethodPtrTy.
3464llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3465 const ObjCMethodDecl *MD) {
3466 // FIXME: Use DenseMap::lookup
3467 llvm::Function *Fn = MethodDefinitions[MD];
3468 if (!Fn)
3469 return 0;
3470
3471 std::vector<llvm::Constant*> Method(3);
3472 Method[0] =
3473 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3474 ObjCTypes.SelectorPtrTy);
3475 Method[1] = GetMethodVarType(MD);
3476 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3477 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3478}
3479
3480/// EmitMethodList - Build meta-data for method declarations
3481/// struct _method_list_t {
3482/// uint32_t entsize; // sizeof(struct _objc_method)
3483/// uint32_t method_count;
3484/// struct _objc_method method_list[method_count];
3485/// }
3486///
3487llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3488 const std::string &Name,
3489 const char *Section,
3490 const ConstantVector &Methods) {
3491 // Return null for empty list.
3492 if (Methods.empty())
3493 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3494
3495 std::vector<llvm::Constant*> Values(3);
3496 // sizeof(struct _objc_method)
3497 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3498 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3499 // method_count
3500 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3501 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3502 Methods.size());
3503 Values[2] = llvm::ConstantArray::get(AT, Methods);
3504 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3505
3506 llvm::GlobalVariable *GV =
3507 new llvm::GlobalVariable(Init->getType(), false,
3508 llvm::GlobalValue::InternalLinkage,
3509 Init,
3510 Name,
3511 &CGM.getModule());
3512 GV->setSection(Section);
3513 UsedGlobals.push_back(GV);
3514 return llvm::ConstantExpr::getBitCast(GV,
3515 ObjCTypes.MethodListnfABIPtrTy);
3516}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003517
3518/// EmitIvarList - Emit the ivar list for the given
3519/// implementation. If ForClass is true the list of class ivars
3520/// (i.e. metaclass ivars) is emitted, otherwise the list of
3521/// interface ivars will be emitted. The return value has type
3522/// IvarListnfABIPtrTy.
3523/// struct _ivar_t {
3524/// unsigned long int *offset; // pointer to ivar offset location
3525/// char *name;
3526/// char *type;
3527/// uint32_t alignment;
3528/// uint32_t size;
3529/// }
3530/// struct _ivar_list_t {
3531/// uint32 entsize; // sizeof(struct _ivar_t)
3532/// uint32 count;
3533/// struct _iver_t list[count];
3534/// }
3535///
3536llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3537 const ObjCImplementationDecl *ID) {
3538
3539 std::vector<llvm::Constant*> Ivars, Ivar(5);
3540
3541 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3542 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3543
3544 for(ObjCInterfaceDecl::ivar_iterator i = OID->ivar_begin(),
3545 e = OID->ivar_end(); i != e; ++i) {
3546 FieldDecl *Field = *i;
3547 // FIXME. Put the offset symbol address after code gen.
3548 // for non-fragile ivar access is in.
3549 Ivar[0] = llvm::Constant::getNullValue(
3550 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3551 if (Field->getIdentifier())
3552 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3553 else
3554 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3555 std::string TypeStr;
3556 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3557 Ivar[2] = GetMethodVarType(TypeStr);
3558 const llvm::Type *FieldTy =
3559 CGM.getTypes().ConvertTypeForMem(Field->getType());
3560 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3561 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3562 Field->getType().getTypePtr()) >> 3;
3563 Align = llvm::Log2_32(Align);
3564 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
3565 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3566 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3567 }
3568 // Return null for empty list.
3569 if (Ivars.empty())
3570 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3571 std::vector<llvm::Constant*> Values(3);
3572 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3573 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3574 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3575 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3576 Ivars.size());
3577 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3578 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3579 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3580 llvm::GlobalVariable *GV =
3581 new llvm::GlobalVariable(Init->getType(), false,
3582 llvm::GlobalValue::InternalLinkage,
3583 Init,
3584 Prefix + OID->getNameAsString(),
3585 &CGM.getModule());
3586
3587 GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3588
3589 UsedGlobals.push_back(GV);
3590 return llvm::ConstantExpr::getBitCast(GV,
3591 ObjCTypes.IvarListnfABIPtrTy);
3592}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003593/* *** */
3594
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003595CodeGen::CGObjCRuntime *
3596CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003597 return new CGObjCMac(CGM);
3598}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003599
3600CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003601CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003602 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003603}