blob: f677782ec3e8235ec2c0f60d7a9c2dee6667f04d [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 Jahanianee0af742009-01-21 22:04:16 +0000356};
357
358class CGObjCMac : public CGObjCCommonMac {
359private:
360 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000361 /// EmitImageInfo - Emit the image info marker used to encode some module
362 /// level information.
363 void EmitImageInfo();
364
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000365 /// EmitModuleInfo - Another marker encoding module level
366 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000367 void EmitModuleInfo();
368
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000369 /// EmitModuleSymols - Emit module symbols, the list of defined
370 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000371 llvm::Constant *EmitModuleSymbols();
372
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000373 /// FinishModule - Write out global data structures at the end of
374 /// processing a translation unit.
375 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000376
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000377 /// EmitClassExtension - Generate the class extension structure used
378 /// to store the weak ivar layout and properties. The return value
379 /// has type ClassExtensionPtrTy.
380 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
381
382 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
383 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000384 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000385 const ObjCInterfaceDecl *ID);
386
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000387 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000388 QualType ResultType,
389 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000390 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000391 QualType Arg0Ty,
392 bool IsSuper,
393 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000394
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000395 /// EmitIvarList - Emit the ivar list for the given
396 /// implementation. If ForClass is true the list of class ivars
397 /// (i.e. metaclass ivars) is emitted, otherwise the list of
398 /// interface ivars will be emitted. The return value has type
399 /// IvarListPtrTy.
400 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
401 bool ForClass,
402 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000403
404 /// EmitMetaClass - Emit a forward reference to the class structure
405 /// for the metaclass of the given interface. The return value has
406 /// type ClassPtrTy.
407 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
408
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000409 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000410 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000411 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
412 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000413 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000414 const ConstantVector &Methods);
415
416 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
417
418 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000419
420 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000421 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000422 llvm::Constant *EmitMethodList(const std::string &Name,
423 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000424 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000425
426 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000427 /// method declarations.
428 /// - TypeName: The name for the type containing the methods.
429 /// - IsProtocol: True iff these methods are for a protocol.
430 /// - ClassMethds: True iff these are class methods.
431 /// - Required: When true, only "required" methods are
432 /// listed. Similarly, when false only "optional" methods are
433 /// listed. For classes this should always be true.
434 /// - begin, end: The method list to output.
435 ///
436 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000437 llvm::Constant *EmitMethodDescList(const std::string &Name,
438 const char *Section,
439 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000440
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000441 /// EmitPropertyList - Emit the given property list. The return
442 /// value has type PropertyListPtrTy.
443 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000444 const Decl *Container,
445 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000446
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000447 /// GetOrEmitProtocol - Get the protocol object for the given
448 /// declaration, emitting it if necessary. The return value has type
449 /// ProtocolPtrTy.
450 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
451
452 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
453 /// object for the given declaration, emitting it if needed. These
454 /// forward references will be filled in with empty bodies if no
455 /// definition is seen. The return value has type ProtocolPtrTy.
456 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
457
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000458 /// EmitProtocolExtension - Generate the protocol extension
459 /// structure used to store optional instance and class methods, and
460 /// protocol properties. The return value has type
461 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000462 llvm::Constant *
463 EmitProtocolExtension(const ObjCProtocolDecl *PD,
464 const ConstantVector &OptInstanceMethods,
465 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000466
467 /// EmitProtocolList - Generate the list of referenced
468 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000469 llvm::Constant *EmitProtocolList(const std::string &Name,
470 ObjCProtocolDecl::protocol_iterator begin,
471 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000472
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000473 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
474 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000475 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000477 /// GetProtocolRef - Return a reference to the internal protocol
478 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000479 /// defined. The return value has type ProtocolPtrTy.
480 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000481
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000482public:
483 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000484
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000485 virtual llvm::Function *ModuleInitFunction();
486
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000487 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000488 QualType ResultType,
489 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000490 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000491 bool IsClassMessage,
492 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000493
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000494 virtual CodeGen::RValue
495 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000496 QualType ResultType,
497 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000498 const ObjCInterfaceDecl *Class,
499 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000500 bool IsClassMessage,
501 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000502
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000503 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000504 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000505
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000506 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000507
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000508 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
509 const ObjCContainerDecl *CD=0);
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 Jahanian058a1b72009-01-24 20:21:50 +0000558
559
560
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000561public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000562 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000563 // FIXME. All stubs for now!
564 virtual llvm::Function *ModuleInitFunction();
565
566 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
567 QualType ResultType,
568 Selector Sel,
569 llvm::Value *Receiver,
570 bool IsClassMessage,
571 const CallArgList &CallArgs)
572 {return RValue::get(0);}
573
574 virtual CodeGen::RValue
575 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
576 QualType ResultType,
577 Selector Sel,
578 const ObjCInterfaceDecl *Class,
579 llvm::Value *Receiver,
580 bool IsClassMessage,
581 const CallArgList &CallArgs){ return RValue::get(0);}
582
583 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
584 const ObjCInterfaceDecl *ID){ return 0; }
585
586 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
587 { return 0; }
588
589 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
590 const ObjCContainerDecl *CD=0)
591 { return 0; }
592
593 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD)
594 { return; }
595
596 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
597
598 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
599 const ObjCProtocolDecl *PD)
600 { return 0; }
601
602 virtual void GenerateProtocol(const ObjCProtocolDecl *PD){ return; }
603
604 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
605 virtual llvm::Function *GetPropertySetFunction()
606 { return 0; }
607 virtual llvm::Function *EnumerationMutationFunction()
608 { return 0; }
609
610 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
611 const Stmt &S)
612 { return; }
613 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
614 const ObjCAtThrowStmt &S)
615 { return; }
616 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
617 llvm::Value *AddrWeakObj)
618 { return 0; }
619 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
620 llvm::Value *src, llvm::Value *dst)
621 { return; }
622 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
623 llvm::Value *src, llvm::Value *dest)
624 { return; }
625 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
626 llvm::Value *src, llvm::Value *dest)
627 { return; }
628 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
629 llvm::Value *src, llvm::Value *dest)
630 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000631};
632
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000633} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000634
635/* *** Helper Functions *** */
636
637/// getConstantGEP() - Help routine to construct simple GEPs.
638static llvm::Constant *getConstantGEP(llvm::Constant *C,
639 unsigned idx0,
640 unsigned idx1) {
641 llvm::Value *Idxs[] = {
642 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
643 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
644 };
645 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
646}
647
648/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000649
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000650CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
651 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000652{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000653 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000654 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000655}
656
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000657/// GetClass - Return a reference to the class for the given interface
658/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000659llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000660 const ObjCInterfaceDecl *ID) {
661 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000662}
663
664/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000665llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000666 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000667}
668
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000669/// Generate a constant CFString object.
670/*
671 struct __builtin_CFString {
672 const int *isa; // point to __CFConstantStringClassReference
673 int flags;
674 const char *str;
675 long length;
676 };
677*/
678
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000679llvm::Constant *CGObjCCommonMac::GenerateConstantString(
680 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000681 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000682}
683
684/// Generates a message send where the super is the receiver. This is
685/// a message send to self with special delivery semantics indicating
686/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000687CodeGen::RValue
688CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000689 QualType ResultType,
690 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000691 const ObjCInterfaceDecl *Class,
692 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000693 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000694 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000695 // Create and init a super structure; this is a (receiver, class)
696 // pair we will pass to objc_msgSendSuper.
697 llvm::Value *ObjCSuper =
698 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
699 llvm::Value *ReceiverAsObject =
700 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
701 CGF.Builder.CreateStore(ReceiverAsObject,
702 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000703
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000704 // If this is a class message the metaclass is passed as the target.
705 llvm::Value *Target;
706 if (IsClassMessage) {
707 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
708 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
709 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
710 Target = Super;
711 } else {
712 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
713 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000714 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
715 // and ObjCTypes types.
716 const llvm::Type *ClassTy =
717 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000718 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000719 CGF.Builder.CreateStore(Target,
720 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
721
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000722 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000723 ObjCSuper, ObjCTypes.SuperPtrCTy,
724 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000725}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000726
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000727/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000728CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000729 QualType ResultType,
730 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000731 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000732 bool IsClassMessage,
733 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000734 llvm::Value *Arg0 =
735 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000736 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000737 Arg0, CGF.getContext().getObjCIdType(),
738 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000739}
740
741CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000742 QualType ResultType,
743 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000744 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000745 QualType Arg0Ty,
746 bool IsSuper,
747 const CallArgList &CallArgs) {
748 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000749 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
750 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
751 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000752 CGF.getContext().getObjCSelType()));
753 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000754
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000755 const llvm::FunctionType *FTy =
756 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
757 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000758
759 llvm::Constant *Fn;
760 if (CGM.ReturnTypeUsesSret(ResultType)) {
761 Fn = ObjCTypes.getSendStretFn(IsSuper);
762 } else if (ResultType->isFloatingType()) {
763 // FIXME: Sadly, this is wrong. This actually depends on the
764 // architecture. This happens to be right for x86-32 though.
765 Fn = ObjCTypes.getSendFpretFn(IsSuper);
766 } else {
767 Fn = ObjCTypes.getSendFn(IsSuper);
768 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000769 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000770 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000771}
772
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000773llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000774 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000775 // FIXME: I don't understand why gcc generates this, or where it is
776 // resolved. Investigate. Its also wasteful to look this up over and
777 // over.
778 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
779
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000780 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
781 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000782}
783
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000784void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
785 // FIXME: We shouldn't need this, the protocol decl should contain
786 // enough information to tell us whether this was a declaration or a
787 // definition.
788 DefinedProtocols.insert(PD->getIdentifier());
789
790 // If we have generated a forward reference to this protocol, emit
791 // it now. Otherwise do nothing, the protocol objects are lazily
792 // emitted.
793 if (Protocols.count(PD->getIdentifier()))
794 GetOrEmitProtocol(PD);
795}
796
797llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
798 if (DefinedProtocols.count(PD->getIdentifier()))
799 return GetOrEmitProtocol(PD);
800 return GetOrEmitProtocolRef(PD);
801}
802
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000803/*
804 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
805 struct _objc_protocol {
806 struct _objc_protocol_extension *isa;
807 char *protocol_name;
808 struct _objc_protocol_list *protocol_list;
809 struct _objc__method_prototype_list *instance_methods;
810 struct _objc__method_prototype_list *class_methods
811 };
812
813 See EmitProtocolExtension().
814*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000815llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
816 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
817
818 // Early exit if a defining object has already been generated.
819 if (Entry && Entry->hasInitializer())
820 return Entry;
821
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000822 // FIXME: I don't understand why gcc generates this, or where it is
823 // resolved. Investigate. Its also wasteful to look this up over and
824 // over.
825 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
826
Chris Lattner8ec03f52008-11-24 03:54:41 +0000827 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000828
829 // Construct method lists.
830 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
831 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
832 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
833 e = PD->instmeth_end(); i != e; ++i) {
834 ObjCMethodDecl *MD = *i;
835 llvm::Constant *C = GetMethodDescriptionConstant(MD);
836 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
837 OptInstanceMethods.push_back(C);
838 } else {
839 InstanceMethods.push_back(C);
840 }
841 }
842
843 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
844 e = PD->classmeth_end(); i != e; ++i) {
845 ObjCMethodDecl *MD = *i;
846 llvm::Constant *C = GetMethodDescriptionConstant(MD);
847 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
848 OptClassMethods.push_back(C);
849 } else {
850 ClassMethods.push_back(C);
851 }
852 }
853
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000854 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000855 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000856 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000857 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000858 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000859 PD->protocol_begin(),
860 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000861 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000862 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
863 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000864 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
865 InstanceMethods);
866 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000867 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
868 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000869 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
870 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000871 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
872 Values);
873
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000874 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000875 // Already created, fix the linkage and update the initializer.
876 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000877 Entry->setInitializer(Init);
878 } else {
879 Entry =
880 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
881 llvm::GlobalValue::InternalLinkage,
882 Init,
883 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
884 &CGM.getModule());
885 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
886 UsedGlobals.push_back(Entry);
887 // FIXME: Is this necessary? Why only for protocol?
888 Entry->setAlignment(4);
889 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000890
891 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000892}
893
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000894llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000895 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
896
897 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000898 // We use the initializer as a marker of whether this is a forward
899 // reference or not. At module finalization we add the empty
900 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000901 Entry =
902 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000903 llvm::GlobalValue::ExternalLinkage,
904 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000905 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000906 &CGM.getModule());
907 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
908 UsedGlobals.push_back(Entry);
909 // FIXME: Is this necessary? Why only for protocol?
910 Entry->setAlignment(4);
911 }
912
913 return Entry;
914}
915
916/*
917 struct _objc_protocol_extension {
918 uint32_t size;
919 struct objc_method_description_list *optional_instance_methods;
920 struct objc_method_description_list *optional_class_methods;
921 struct objc_property_list *instance_properties;
922 };
923*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000924llvm::Constant *
925CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
926 const ConstantVector &OptInstanceMethods,
927 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000929 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000930 std::vector<llvm::Constant*> Values(4);
931 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000932 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000933 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
934 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000935 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
936 OptInstanceMethods);
937 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000938 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
939 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000940 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
941 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000942 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
943 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000944 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000945
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000946 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000947 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
948 Values[3]->isNullValue())
949 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
950
951 llvm::Constant *Init =
952 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
953 llvm::GlobalVariable *GV =
954 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
955 llvm::GlobalValue::InternalLinkage,
956 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000957 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000958 &CGM.getModule());
959 // No special section, but goes in llvm.used
960 UsedGlobals.push_back(GV);
961
962 return GV;
963}
964
965/*
966 struct objc_protocol_list {
967 struct objc_protocol_list *next;
968 long count;
969 Protocol *list[];
970 };
971*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000972llvm::Constant *
973CGObjCMac::EmitProtocolList(const std::string &Name,
974 ObjCProtocolDecl::protocol_iterator begin,
975 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000976 std::vector<llvm::Constant*> ProtocolRefs;
977
Daniel Dunbardbc933702008-08-21 21:57:41 +0000978 for (; begin != end; ++begin)
979 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000980
981 // Just return null for empty protocol lists
982 if (ProtocolRefs.empty())
983 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
984
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000985 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000986 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
987
988 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000989 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
991 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
992 Values[2] =
993 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
994 ProtocolRefs.size()),
995 ProtocolRefs);
996
997 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
998 llvm::GlobalVariable *GV =
999 new llvm::GlobalVariable(Init->getType(), false,
1000 llvm::GlobalValue::InternalLinkage,
1001 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001002 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001003 &CGM.getModule());
1004 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1005 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1006}
1007
1008/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001009 struct _objc_property {
1010 const char * const name;
1011 const char * const attributes;
1012 };
1013
1014 struct _objc_property_list {
1015 uint32_t entsize; // sizeof (struct _objc_property)
1016 uint32_t prop_count;
1017 struct _objc_property[prop_count];
1018 };
1019*/
1020llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001021 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +00001022 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001023 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001024 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1025 E = OCD->prop_end(); I != E; ++I) {
1026 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001027 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001028 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001029 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1030 Prop));
1031 }
1032
1033 // Return null for empty list.
1034 if (Properties.empty())
1035 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1036
1037 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001038 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001039 std::vector<llvm::Constant*> Values(3);
1040 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1041 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1042 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1043 Properties.size());
1044 Values[2] = llvm::ConstantArray::get(AT, Properties);
1045 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1046
1047 llvm::GlobalVariable *GV =
1048 new llvm::GlobalVariable(Init->getType(), false,
1049 llvm::GlobalValue::InternalLinkage,
1050 Init,
1051 Name,
1052 &CGM.getModule());
1053 // No special section on property lists?
1054 UsedGlobals.push_back(GV);
1055 return llvm::ConstantExpr::getBitCast(GV,
1056 ObjCTypes.PropertyListPtrTy);
1057
1058}
1059
1060/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001061 struct objc_method_description_list {
1062 int count;
1063 struct objc_method_description list[];
1064 };
1065*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001066llvm::Constant *
1067CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1068 std::vector<llvm::Constant*> Desc(2);
1069 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1070 ObjCTypes.SelectorPtrTy);
1071 Desc[1] = GetMethodVarType(MD);
1072 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1073 Desc);
1074}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001076llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1077 const char *Section,
1078 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001079 // Return null for empty list.
1080 if (Methods.empty())
1081 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1082
1083 std::vector<llvm::Constant*> Values(2);
1084 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1085 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1086 Methods.size());
1087 Values[1] = llvm::ConstantArray::get(AT, Methods);
1088 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1089
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001090 llvm::GlobalVariable *GV =
1091 new llvm::GlobalVariable(Init->getType(), false,
1092 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001093 Init, Name, &CGM.getModule());
1094 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001095 UsedGlobals.push_back(GV);
1096 return llvm::ConstantExpr::getBitCast(GV,
1097 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001098}
1099
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001100/*
1101 struct _objc_category {
1102 char *category_name;
1103 char *class_name;
1104 struct _objc_method_list *instance_methods;
1105 struct _objc_method_list *class_methods;
1106 struct _objc_protocol_list *protocols;
1107 uint32_t size; // <rdar://4585769>
1108 struct _objc_property_list *instance_properties;
1109 };
1110 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001111void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001112 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001113
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001114 // FIXME: This is poor design, the OCD should have a pointer to the
1115 // category decl. Additionally, note that Category can be null for
1116 // the @implementation w/o an @interface case. Sema should just
1117 // create one for us as it does for @implementation so everyone else
1118 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001119 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001120 const ObjCCategoryDecl *Category =
1121 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001122 std::string ExtName(Interface->getNameAsString() + "_" +
1123 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001124
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1126 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1127 e = OCD->instmeth_end(); i != e; ++i) {
1128 // Instance methods should always be defined.
1129 InstanceMethods.push_back(GetMethodConstant(*i));
1130 }
1131 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1132 e = OCD->classmeth_end(); i != e; ++i) {
1133 // Class methods should always be defined.
1134 ClassMethods.push_back(GetMethodConstant(*i));
1135 }
1136
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001137 std::vector<llvm::Constant*> Values(7);
1138 Values[0] = GetClassName(OCD->getIdentifier());
1139 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001140 Values[2] =
1141 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1142 ExtName,
1143 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001144 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001145 Values[3] =
1146 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1147 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001148 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001149 if (Category) {
1150 Values[4] =
1151 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1152 Category->protocol_begin(),
1153 Category->protocol_end());
1154 } else {
1155 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1156 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001157 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001158
1159 // If there is no category @interface then there can be no properties.
1160 if (Category) {
1161 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001162 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001163 } else {
1164 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1165 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001166
1167 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1168 Values);
1169
1170 llvm::GlobalVariable *GV =
1171 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1172 llvm::GlobalValue::InternalLinkage,
1173 Init,
1174 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1175 &CGM.getModule());
1176 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1177 UsedGlobals.push_back(GV);
1178 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001179}
1180
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001181// FIXME: Get from somewhere?
1182enum ClassFlags {
1183 eClassFlags_Factory = 0x00001,
1184 eClassFlags_Meta = 0x00002,
1185 // <rdr://5142207>
1186 eClassFlags_HasCXXStructors = 0x02000,
1187 eClassFlags_Hidden = 0x20000,
1188 eClassFlags_ABI2_Hidden = 0x00010,
1189 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1190};
1191
1192// <rdr://5142207&4705298&4843145>
1193static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1194 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1195 // FIXME: Support -fvisibility
1196 switch (attr->getVisibility()) {
1197 default:
1198 assert(0 && "Unknown visibility");
1199 return false;
1200 case VisibilityAttr::DefaultVisibility:
1201 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1202 return false;
1203 case VisibilityAttr::HiddenVisibility:
1204 return true;
1205 }
1206 } else {
1207 return false; // FIXME: Support -fvisibility
1208 }
1209}
1210
1211/*
1212 struct _objc_class {
1213 Class isa;
1214 Class super_class;
1215 const char *name;
1216 long version;
1217 long info;
1218 long instance_size;
1219 struct _objc_ivar_list *ivars;
1220 struct _objc_method_list *methods;
1221 struct _objc_cache *cache;
1222 struct _objc_protocol_list *protocols;
1223 // Objective-C 1.0 extensions (<rdr://4585769>)
1224 const char *ivar_layout;
1225 struct _objc_class_ext *ext;
1226 };
1227
1228 See EmitClassExtension();
1229 */
1230void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001231 DefinedSymbols.insert(ID->getIdentifier());
1232
Chris Lattner8ec03f52008-11-24 03:54:41 +00001233 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001234 // FIXME: Gross
1235 ObjCInterfaceDecl *Interface =
1236 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001237 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001238 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001239 Interface->protocol_begin(),
1240 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001241 const llvm::Type *InterfaceTy =
1242 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1243 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001244 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001245
1246 // FIXME: Set CXX-structors flag.
1247 if (IsClassHidden(ID->getClassInterface()))
1248 Flags |= eClassFlags_Hidden;
1249
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001250 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1251 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1252 e = ID->instmeth_end(); i != e; ++i) {
1253 // Instance methods should always be defined.
1254 InstanceMethods.push_back(GetMethodConstant(*i));
1255 }
1256 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1257 e = ID->classmeth_end(); i != e; ++i) {
1258 // Class methods should always be defined.
1259 ClassMethods.push_back(GetMethodConstant(*i));
1260 }
1261
1262 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1263 e = ID->propimpl_end(); i != e; ++i) {
1264 ObjCPropertyImplDecl *PID = *i;
1265
1266 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1267 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1268
1269 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1270 if (llvm::Constant *C = GetMethodConstant(MD))
1271 InstanceMethods.push_back(C);
1272 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1273 if (llvm::Constant *C = GetMethodConstant(MD))
1274 InstanceMethods.push_back(C);
1275 }
1276 }
1277
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001278 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001279 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001280 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001281 // Record a reference to the super class.
1282 LazySymbols.insert(Super->getIdentifier());
1283
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001284 Values[ 1] =
1285 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1286 ObjCTypes.ClassPtrTy);
1287 } else {
1288 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1289 }
1290 Values[ 2] = GetClassName(ID->getIdentifier());
1291 // Version is always 0.
1292 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1293 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1294 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1295 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001296 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001297 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001298 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001299 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001300 // cache is always NULL.
1301 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1302 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001303 // FIXME: Set ivar_layout
1304 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001305 Values[11] = EmitClassExtension(ID);
1306 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1307 Values);
1308
1309 llvm::GlobalVariable *GV =
1310 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1311 llvm::GlobalValue::InternalLinkage,
1312 Init,
1313 std::string("\01L_OBJC_CLASS_")+ClassName,
1314 &CGM.getModule());
1315 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1316 UsedGlobals.push_back(GV);
1317 // FIXME: Why?
1318 GV->setAlignment(32);
1319 DefinedClasses.push_back(GV);
1320}
1321
1322llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1323 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001324 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001325 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001326 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001327 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001328
1329 if (IsClassHidden(ID->getClassInterface()))
1330 Flags |= eClassFlags_Hidden;
1331
1332 std::vector<llvm::Constant*> Values(12);
1333 // The isa for the metaclass is the root of the hierarchy.
1334 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1335 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1336 Root = Super;
1337 Values[ 0] =
1338 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1339 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001340 // The super class for the metaclass is emitted as the name of the
1341 // super class. The runtime fixes this up to point to the
1342 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001343 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1344 Values[ 1] =
1345 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1346 ObjCTypes.ClassPtrTy);
1347 } else {
1348 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1349 }
1350 Values[ 2] = GetClassName(ID->getIdentifier());
1351 // Version is always 0.
1352 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1353 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1354 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1355 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001356 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001357 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001358 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001359 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001360 // cache is always NULL.
1361 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1362 Values[ 9] = Protocols;
1363 // ivar_layout for metaclass is always NULL.
1364 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1365 // The class extension is always unused for metaclasses.
1366 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1367 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1368 Values);
1369
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001370 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001371 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001372
1373 // Check for a forward reference.
1374 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1375 if (GV) {
1376 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1377 "Forward metaclass reference has incorrect type.");
1378 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1379 GV->setInitializer(Init);
1380 } else {
1381 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1382 llvm::GlobalValue::InternalLinkage,
1383 Init, Name,
1384 &CGM.getModule());
1385 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001386 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1387 UsedGlobals.push_back(GV);
1388 // FIXME: Why?
1389 GV->setAlignment(32);
1390
1391 return GV;
1392}
1393
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001394llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001395 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001396
1397 // FIXME: Should we look these up somewhere other than the
1398 // module. Its a bit silly since we only generate these while
1399 // processing an implementation, so exactly one pointer would work
1400 // if know when we entered/exitted an implementation block.
1401
1402 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001403 // Previously, metaclass with internal linkage may have been defined.
1404 // pass 'true' as 2nd argument so it is returned.
1405 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001406 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1407 "Forward metaclass reference has incorrect type.");
1408 return GV;
1409 } else {
1410 // Generate as an external reference to keep a consistent
1411 // module. This will be patched up when we emit the metaclass.
1412 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1413 llvm::GlobalValue::ExternalLinkage,
1414 0,
1415 Name,
1416 &CGM.getModule());
1417 }
1418}
1419
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001420/*
1421 struct objc_class_ext {
1422 uint32_t size;
1423 const char *weak_ivar_layout;
1424 struct _objc_property_list *properties;
1425 };
1426*/
1427llvm::Constant *
1428CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1429 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001430 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431
1432 std::vector<llvm::Constant*> Values(3);
1433 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001434 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001436 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001437 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001438
1439 // Return null if no extension bits are used.
1440 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1441 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1442
1443 llvm::Constant *Init =
1444 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1445 llvm::GlobalVariable *GV =
1446 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1447 llvm::GlobalValue::InternalLinkage,
1448 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001449 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001450 &CGM.getModule());
1451 // No special section, but goes in llvm.used
1452 UsedGlobals.push_back(GV);
1453
1454 return GV;
1455}
1456
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001457/// countInheritedIvars - count number of ivars in class and its super class(s)
1458///
1459static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1460 int count = 0;
1461 if (!OI)
1462 return 0;
1463 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1464 if (SuperClass)
1465 count += countInheritedIvars(SuperClass);
1466 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1467 E = OI->ivar_end(); I != E; ++I)
1468 ++count;
1469 return count;
1470}
1471
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001472/*
1473 struct objc_ivar {
1474 char *ivar_name;
1475 char *ivar_type;
1476 int ivar_offset;
1477 };
1478
1479 struct objc_ivar_list {
1480 int ivar_count;
1481 struct objc_ivar list[count];
1482 };
1483 */
1484llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1485 bool ForClass,
1486 const llvm::Type *InterfaceTy) {
1487 std::vector<llvm::Constant*> Ivars, Ivar(3);
1488
1489 // When emitting the root class GCC emits ivar entries for the
1490 // actual class structure. It is not clear if we need to follow this
1491 // behavior; for now lets try and get away with not doing it. If so,
1492 // the cleanest solution would be to make up an ObjCInterfaceDecl
1493 // for the class.
1494 if (ForClass)
1495 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1496
1497 const llvm::StructLayout *Layout =
1498 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001499 ObjCInterfaceDecl *OID =
1500 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1501 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1502 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1503 RecordDecl::field_iterator ifield = RD->field_begin();
1504 while (countSuperClassIvars-- > 0)
1505 ++ifield;
1506 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1507 FieldDecl *Field = *ifield;
1508 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1509 getLLVMFieldNo(Field));
1510 if (Field->getIdentifier())
1511 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1512 else
1513 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001514 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001515 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516 Ivar[1] = GetMethodVarType(TypeStr);
1517 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001518 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001519 }
1520
1521 // Return null for empty list.
1522 if (Ivars.empty())
1523 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1524
1525 std::vector<llvm::Constant*> Values(2);
1526 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1527 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1528 Ivars.size());
1529 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1530 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1531
1532 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1533 "\01L_OBJC_INSTANCE_VARIABLES_");
1534 llvm::GlobalVariable *GV =
1535 new llvm::GlobalVariable(Init->getType(), false,
1536 llvm::GlobalValue::InternalLinkage,
1537 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001538 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001539 &CGM.getModule());
1540 if (ForClass) {
1541 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1542 // FIXME: Why is this only here?
1543 GV->setAlignment(32);
1544 } else {
1545 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1546 }
1547 UsedGlobals.push_back(GV);
1548 return llvm::ConstantExpr::getBitCast(GV,
1549 ObjCTypes.IvarListPtrTy);
1550}
1551
1552/*
1553 struct objc_method {
1554 SEL method_name;
1555 char *method_types;
1556 void *method;
1557 };
1558
1559 struct objc_method_list {
1560 struct objc_method_list *obsolete;
1561 int count;
1562 struct objc_method methods_list[count];
1563 };
1564*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001565
1566/// GetMethodConstant - Return a struct objc_method constant for the
1567/// given method if it has been defined. The result is null if the
1568/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001569llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001570 // FIXME: Use DenseMap::lookup
1571 llvm::Function *Fn = MethodDefinitions[MD];
1572 if (!Fn)
1573 return 0;
1574
1575 std::vector<llvm::Constant*> Method(3);
1576 Method[0] =
1577 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1578 ObjCTypes.SelectorPtrTy);
1579 Method[1] = GetMethodVarType(MD);
1580 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1581 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1582}
1583
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001584llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1585 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001586 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001587 // Return null for empty list.
1588 if (Methods.empty())
1589 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1590
1591 std::vector<llvm::Constant*> Values(3);
1592 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1593 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1594 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1595 Methods.size());
1596 Values[2] = llvm::ConstantArray::get(AT, Methods);
1597 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1598
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001599 llvm::GlobalVariable *GV =
1600 new llvm::GlobalVariable(Init->getType(), false,
1601 llvm::GlobalValue::InternalLinkage,
1602 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001603 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001604 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001605 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001606 UsedGlobals.push_back(GV);
1607 return llvm::ConstantExpr::getBitCast(GV,
1608 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001609}
1610
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001611llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD,
1612 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001613 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001614 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001615
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001616 const llvm::FunctionType *MethodTy =
1617 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001618 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001619 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001620 llvm::GlobalValue::InternalLinkage,
1621 Name,
1622 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001623 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001624
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001625 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001626}
1627
1628llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001629 // Abuse this interface function as a place to finalize.
1630 FinishModule();
1631
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001632 return NULL;
1633}
1634
Daniel Dunbar49f66022008-09-24 03:38:44 +00001635llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1636 return ObjCTypes.GetPropertyFn;
1637}
1638
1639llvm::Function *CGObjCMac::GetPropertySetFunction() {
1640 return ObjCTypes.SetPropertyFn;
1641}
1642
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001643llvm::Function *CGObjCMac::EnumerationMutationFunction()
1644{
1645 return ObjCTypes.EnumerationMutationFn;
1646}
1647
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001648/*
1649
1650Objective-C setjmp-longjmp (sjlj) Exception Handling
1651--
1652
1653The basic framework for a @try-catch-finally is as follows:
1654{
1655 objc_exception_data d;
1656 id _rethrow = null;
1657
1658 objc_exception_try_enter(&d);
1659 if (!setjmp(d.jmp_buf)) {
1660 ... try body ...
1661 } else {
1662 // exception path
1663 id _caught = objc_exception_extract(&d);
1664
1665 // enter new try scope for handlers
1666 if (!setjmp(d.jmp_buf)) {
1667 ... match exception and execute catch blocks ...
1668
1669 // fell off end, rethrow.
1670 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001671 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001672 } else {
1673 // exception in catch block
1674 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001675 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001676 }
1677 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001678 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001679
1680finally:
1681 // match either the initial try_enter or the catch try_enter,
1682 // depending on the path followed.
1683 objc_exception_try_exit(&d);
1684finally_no_exit:
1685 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001686 ... dispatch to finally destination ...
1687
1688finally_rethrow:
1689 objc_exception_throw(_rethrow);
1690
1691finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001692}
1693
1694This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001695uses _rethrow to determine if objc_exception_try_exit should be called
1696and if the object should be rethrown. This breaks in the face of
1697throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001698
1699We specialize this framework for a few particular circumstances:
1700
1701 - If there are no catch blocks, then we avoid emitting the second
1702 exception handling context.
1703
1704 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1705 e)) we avoid emitting the code to rethrow an uncaught exception.
1706
1707 - FIXME: If there is no @finally block we can do a few more
1708 simplifications.
1709
1710Rethrows and Jumps-Through-Finally
1711--
1712
1713Support for implicit rethrows and jumping through the finally block is
1714handled by storing the current exception-handling context in
1715ObjCEHStack.
1716
Daniel Dunbar898d5082008-09-30 01:06:03 +00001717In order to implement proper @finally semantics, we support one basic
1718mechanism for jumping through the finally block to an arbitrary
1719destination. Constructs which generate exits from a @try or @catch
1720block use this mechanism to implement the proper semantics by chaining
1721jumps, as necessary.
1722
1723This mechanism works like the one used for indirect goto: we
1724arbitrarily assign an ID to each destination and store the ID for the
1725destination in a variable prior to entering the finally block. At the
1726end of the finally block we simply create a switch to the proper
1727destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001728
1729Code gen for @synchronized(expr) stmt;
1730Effectively generating code for:
1731objc_sync_enter(expr);
1732@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001733*/
1734
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001735void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1736 const Stmt &S) {
1737 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001738 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001739 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1740 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1741 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1742 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001743 llvm::Value *DestCode =
1744 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1745
1746 // Generate jump code. Done here so we can directly add things to
1747 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001748 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001749 llvm::SwitchInst *FinallySwitch =
1750 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1751 FinallyEnd, 10, FinallyJump);
1752
1753 // Push an EH context entry, used for handling rethrows and jumps
1754 // through finally.
1755 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1756 FinallySwitch, DestCode);
1757 CGF.ObjCEHStack.push_back(&EHEntry);
1758
1759 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001760 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1761 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001762 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1763 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001764 if (!isTry) {
1765 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001766 llvm::Value *Arg = CGF.EmitScalarExpr(
1767 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1768 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1769 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001770 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001771
1772 // Enter a new try block and call setjmp.
1773 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1774 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1775 "jmpbufarray");
1776 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1777 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1778 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001779
Daniel Dunbar55e87422008-11-11 02:29:29 +00001780 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1781 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001782 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001783 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001784
1785 // Emit the @try block.
1786 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001787 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1788 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001789 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001790
1791 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001792 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001793
1794 // Retrieve the exception object. We may emit multiple blocks but
1795 // nothing can cross this so the value is already in SSA form.
1796 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1797 ExceptionData,
1798 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001799 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001800 if (!isTry)
1801 {
1802 CGF.Builder.CreateStore(Caught, RethrowPtr);
1803 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1804 }
1805 else if (const ObjCAtCatchStmt* CatchStmt =
1806 cast<ObjCAtTryStmt>(S).getCatchStmts())
1807 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001808 // Enter a new exception try block (in case a @catch block throws
1809 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001810 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001811
Anders Carlsson80f25672008-09-09 17:59:25 +00001812 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1813 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001814 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001815
Daniel Dunbar55e87422008-11-11 02:29:29 +00001816 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1817 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001818 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001819
1820 CGF.EmitBlock(CatchBlock);
1821
Daniel Dunbar55e40722008-09-27 07:03:52 +00001822 // Handle catch list. As a special case we check if everything is
1823 // matched and avoid generating code for falling off the end if
1824 // so.
1825 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001826 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001827 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001828
Anders Carlssondde0a942008-09-11 09:15:33 +00001829 const DeclStmt *CatchParam =
1830 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001831 const VarDecl *VD = 0;
1832 const PointerType *PT = 0;
1833
Anders Carlsson80f25672008-09-09 17:59:25 +00001834 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001835 if (!CatchParam) {
1836 AllMatched = true;
1837 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001838 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001839 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001840
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001841 // catch(id e) always matches.
1842 // FIXME: For the time being we also match id<X>; this should
1843 // be rejected by Sema instead.
1844 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1845 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001846 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001847 }
1848
Daniel Dunbar55e40722008-09-27 07:03:52 +00001849 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001850 if (CatchParam) {
1851 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001852 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001853 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001854 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001855
Anders Carlssondde0a942008-09-11 09:15:33 +00001856 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001857 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001858 break;
1859 }
1860
Daniel Dunbar129271a2008-09-27 07:36:24 +00001861 assert(PT && "Unexpected non-pointer type in @catch");
1862 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001863 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001864 assert(ObjCType && "Catch parameter must have Objective-C type!");
1865
1866 // Check if the @catch block matches the exception object.
1867 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1868
Anders Carlsson80f25672008-09-09 17:59:25 +00001869 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1870 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001871
Daniel Dunbar55e87422008-11-11 02:29:29 +00001872 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001873
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001874 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001875 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001876
1877 // Emit the @catch block.
1878 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001879 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001880 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001881
1882 llvm::Value *Tmp =
1883 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1884 "tmp");
1885 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001886
1887 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001888 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001889
1890 CGF.EmitBlock(NextCatchBlock);
1891 }
1892
Daniel Dunbar55e40722008-09-27 07:03:52 +00001893 if (!AllMatched) {
1894 // None of the handlers caught the exception, so store it to be
1895 // rethrown at the end of the @finally block.
1896 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001897 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001898 }
1899
1900 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001901 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001902 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1903 ExceptionData),
1904 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001905 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001906 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001907 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001908 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001909 }
1910
Daniel Dunbar898d5082008-09-30 01:06:03 +00001911 // Pop the exception-handling stack entry. It is important to do
1912 // this now, because the code in the @finally block is not in this
1913 // context.
1914 CGF.ObjCEHStack.pop_back();
1915
Anders Carlsson80f25672008-09-09 17:59:25 +00001916 // Emit the @finally block.
1917 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001918 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001919
1920 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001921 if (isTry) {
1922 if (const ObjCAtFinallyStmt* FinallyStmt =
1923 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1924 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1925 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001926 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001927 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001928 // For @synchronized, call objc_sync_enter(sync.expr)
1929 llvm::Value *Arg = CGF.EmitScalarExpr(
1930 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1931 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1932 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1933 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001934
Daniel Dunbar898d5082008-09-30 01:06:03 +00001935 CGF.EmitBlock(FinallyJump);
1936
1937 CGF.EmitBlock(FinallyRethrow);
1938 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1939 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001940 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001941
1942 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001943}
1944
1945void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001946 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001947 llvm::Value *ExceptionAsObject;
1948
1949 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1950 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1951 ExceptionAsObject =
1952 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1953 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001954 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001955 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001956 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001957 }
1958
1959 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001960 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001961
1962 // Clear the insertion point to indicate we are in unreachable code.
1963 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001964}
1965
Daniel Dunbar898d5082008-09-30 01:06:03 +00001966void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1967 llvm::BasicBlock *Dst,
1968 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001969 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001970 return;
1971
1972 // Find the destination code for this block. We always use 0 for the
1973 // fallthrough block (default destination).
1974 llvm::SwitchInst *SI = E->FinallySwitch;
1975 llvm::ConstantInt *ID;
1976 if (Dst == SI->getDefaultDest()) {
1977 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1978 } else {
1979 ID = SI->findCaseDest(Dst);
1980 if (!ID) {
1981 // No code found, get a new unique one by just using the number
1982 // of switch successors.
1983 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1984 SI->addCase(ID, Dst);
1985 }
1986 }
1987
1988 // Set the destination code and branch.
1989 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001990 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001991}
1992
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001993/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001994/// object: objc_read_weak (id *src)
1995///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001996llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001997 llvm::Value *AddrWeakObj)
1998{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001999 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002000 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002001 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002002 return read_weak;
2003}
2004
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002005/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2006/// objc_assign_weak (id src, id *dst)
2007///
2008void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2009 llvm::Value *src, llvm::Value *dst)
2010{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002011 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2012 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002013 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2014 src, dst, "weakassign");
2015 return;
2016}
2017
Fariborz Jahanian58626502008-11-19 00:59:10 +00002018/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2019/// objc_assign_global (id src, id *dst)
2020///
2021void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2022 llvm::Value *src, llvm::Value *dst)
2023{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002024 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2025 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002026 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2027 src, dst, "globalassign");
2028 return;
2029}
2030
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002031/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2032/// objc_assign_ivar (id src, id *dst)
2033///
2034void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2035 llvm::Value *src, llvm::Value *dst)
2036{
2037 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2038 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2039 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2040 src, dst, "assignivar");
2041 return;
2042}
2043
Fariborz Jahanian58626502008-11-19 00:59:10 +00002044/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2045/// objc_assign_strongCast (id src, id *dst)
2046///
2047void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2048 llvm::Value *src, llvm::Value *dst)
2049{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002050 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2051 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002052 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2053 src, dst, "weakassign");
2054 return;
2055}
2056
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002057/* *** Private Interface *** */
2058
2059/// EmitImageInfo - Emit the image info marker used to encode some module
2060/// level information.
2061///
2062/// See: <rdr://4810609&4810587&4810587>
2063/// struct IMAGE_INFO {
2064/// unsigned version;
2065/// unsigned flags;
2066/// };
2067enum ImageInfoFlags {
2068 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2069 eImageInfo_GarbageCollected = (1 << 1),
2070 eImageInfo_GCOnly = (1 << 2)
2071};
2072
2073void CGObjCMac::EmitImageInfo() {
2074 unsigned version = 0; // Version is unused?
2075 unsigned flags = 0;
2076
2077 // FIXME: Fix and continue?
2078 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2079 flags |= eImageInfo_GarbageCollected;
2080 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2081 flags |= eImageInfo_GCOnly;
2082
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002083 // Emitted as int[2];
2084 llvm::Constant *values[2] = {
2085 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2086 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2087 };
2088 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002089 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002090 new llvm::GlobalVariable(AT, true,
2091 llvm::GlobalValue::InternalLinkage,
2092 llvm::ConstantArray::get(AT, values, 2),
2093 "\01L_OBJC_IMAGE_INFO",
2094 &CGM.getModule());
2095
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002096 if (ObjCABI == 1) {
2097 GV->setSection("__OBJC, __image_info,regular");
2098 } else {
2099 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2100 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002101
2102 UsedGlobals.push_back(GV);
2103}
2104
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002105
2106// struct objc_module {
2107// unsigned long version;
2108// unsigned long size;
2109// const char *name;
2110// Symtab symtab;
2111// };
2112
2113// FIXME: Get from somewhere
2114static const int ModuleVersion = 7;
2115
2116void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002117 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002118
2119 std::vector<llvm::Constant*> Values(4);
2120 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2121 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002122 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002123 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002124 Values[3] = EmitModuleSymbols();
2125
2126 llvm::GlobalVariable *GV =
2127 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2128 llvm::GlobalValue::InternalLinkage,
2129 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2130 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002131 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002132 &CGM.getModule());
2133 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2134 UsedGlobals.push_back(GV);
2135}
2136
2137llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002138 unsigned NumClasses = DefinedClasses.size();
2139 unsigned NumCategories = DefinedCategories.size();
2140
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002141 // Return null if no symbols were defined.
2142 if (!NumClasses && !NumCategories)
2143 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2144
2145 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002146 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2147 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2148 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2149 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2150
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002151 // The runtime expects exactly the list of defined classes followed
2152 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002154 for (unsigned i=0; i<NumClasses; i++)
2155 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2156 ObjCTypes.Int8PtrTy);
2157 for (unsigned i=0; i<NumCategories; i++)
2158 Symbols[NumClasses + i] =
2159 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2160 ObjCTypes.Int8PtrTy);
2161
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002162 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002163 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164 NumClasses + NumCategories),
2165 Symbols);
2166
2167 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2168
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002169 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002171 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002172 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002173 "\01L_OBJC_SYMBOLS",
2174 &CGM.getModule());
2175 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2176 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2178}
2179
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002180llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002182 LazySymbols.insert(ID->getIdentifier());
2183
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2185
2186 if (!Entry) {
2187 llvm::Constant *Casted =
2188 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2189 ObjCTypes.ClassPtrTy);
2190 Entry =
2191 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2192 llvm::GlobalValue::InternalLinkage,
2193 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2194 &CGM.getModule());
2195 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2196 UsedGlobals.push_back(Entry);
2197 }
2198
2199 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002200}
2201
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002202llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002203 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2204
2205 if (!Entry) {
2206 llvm::Constant *Casted =
2207 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2208 ObjCTypes.SelectorPtrTy);
2209 Entry =
2210 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2211 llvm::GlobalValue::InternalLinkage,
2212 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2213 &CGM.getModule());
2214 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2215 UsedGlobals.push_back(Entry);
2216 }
2217
2218 return Builder.CreateLoad(Entry, false, "tmp");
2219}
2220
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002221llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002222 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002223
2224 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002225 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002226 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002227 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002228 llvm::GlobalValue::InternalLinkage,
2229 C, "\01L_OBJC_CLASS_NAME_",
2230 &CGM.getModule());
2231 Entry->setSection("__TEXT,__cstring,cstring_literals");
2232 UsedGlobals.push_back(Entry);
2233 }
2234
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002235 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002236}
2237
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002238llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002239 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2240
2241 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002242 // FIXME: Avoid std::string copying.
2243 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002244 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002245 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002246 llvm::GlobalValue::InternalLinkage,
2247 C, "\01L_OBJC_METH_VAR_NAME_",
2248 &CGM.getModule());
2249 Entry->setSection("__TEXT,__cstring,cstring_literals");
2250 UsedGlobals.push_back(Entry);
2251 }
2252
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002253 return getConstantGEP(Entry, 0, 0);
2254}
2255
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002257llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2259}
2260
2261// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002262llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2264}
2265
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002266llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002268
2269 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002271 Entry =
2272 new llvm::GlobalVariable(C->getType(), false,
2273 llvm::GlobalValue::InternalLinkage,
2274 C, "\01L_OBJC_METH_VAR_TYPE_",
2275 &CGM.getModule());
2276 Entry->setSection("__TEXT,__cstring,cstring_literals");
2277 UsedGlobals.push_back(Entry);
2278 }
2279
2280 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002281}
2282
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002284llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002285 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002286 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2287 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002288 return GetMethodVarType(TypeStr);
2289}
2290
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002291// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002292llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002293 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2294
2295 if (!Entry) {
2296 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2297 Entry =
2298 new llvm::GlobalVariable(C->getType(), false,
2299 llvm::GlobalValue::InternalLinkage,
2300 C, "\01L_OBJC_PROP_NAME_ATTR_",
2301 &CGM.getModule());
2302 Entry->setSection("__TEXT,__cstring,cstring_literals");
2303 UsedGlobals.push_back(Entry);
2304 }
2305
2306 return getConstantGEP(Entry, 0, 0);
2307}
2308
2309// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002310// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002311llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002312 const Decl *Container) {
2313 std::string TypeStr;
2314 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002315 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2316}
2317
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002318void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2319 const ObjCContainerDecl *CD,
2320 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002321 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002322 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002323 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002324 assert (CD && "Missing container decl in GetNameForMethod");
2325 NameOut += CD->getNameAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +00002326 NameOut += ' ';
2327 NameOut += D->getSelector().getAsString();
2328 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002329}
2330
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002331void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002332 EmitModuleInfo();
2333
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002334 // Emit the dummy bodies for any protocols which were referenced but
2335 // never defined.
2336 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2337 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2338 if (i->second->hasInitializer())
2339 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002340
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002341 std::vector<llvm::Constant*> Values(5);
2342 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2343 Values[1] = GetClassName(i->first);
2344 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2345 Values[3] = Values[4] =
2346 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2347 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2348 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2349 Values));
2350 }
2351
2352 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002353 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002354 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002356 }
2357
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002359 llvm::GlobalValue *GV =
2360 new llvm::GlobalVariable(AT, false,
2361 llvm::GlobalValue::AppendingLinkage,
2362 llvm::ConstantArray::get(AT, Used),
2363 "llvm.used",
2364 &CGM.getModule());
2365
2366 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002367
2368 // Add assembler directives to add lazy undefined symbol references
2369 // for classes which are referenced but not defined. This is
2370 // important for correct linker interaction.
2371
2372 // FIXME: Uh, this isn't particularly portable.
2373 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002374
2375 if (!CGM.getModule().getModuleInlineAsm().empty())
2376 s << "\n";
2377
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002378 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2379 e = LazySymbols.end(); i != e; ++i) {
2380 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2381 }
2382 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2383 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002384 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002385 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2386 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002387
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002388 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002389}
2390
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002391CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002392 : CGObjCCommonMac(cgm),
2393 ObjCTypes(cgm)
2394{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002395 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002396 ObjCABI = 2;
2397}
2398
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002399/* *** */
2400
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002401ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2402: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002403{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002404 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2405 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002406
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002407 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002408 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002409 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002410 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2411
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002412 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002413 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002414 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002415
2416 // FIXME: It would be nice to unify this with the opaque type, so
2417 // that the IR comes out a bit cleaner.
2418 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2419 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002420
2421 // I'm not sure I like this. The implicit coordination is a bit
2422 // gross. We should solve this in a reasonable fashion because this
2423 // is a pretty common task (match some runtime data structure with
2424 // an LLVM data structure).
2425
2426 // FIXME: This is leaked.
2427 // FIXME: Merge with rewriter code?
2428
2429 // struct _objc_super {
2430 // id self;
2431 // Class cls;
2432 // }
2433 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2434 SourceLocation(),
2435 &Ctx.Idents.get("_objc_super"));
2436 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2437 Ctx.getObjCIdType(), 0, false));
2438 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2439 Ctx.getObjCClassType(), 0, false));
2440 RD->completeDefinition(Ctx);
2441
2442 SuperCTy = Ctx.getTagDeclType(RD);
2443 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2444
2445 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002446 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2447
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002448 // struct _prop_t {
2449 // char *name;
2450 // char *attributes;
2451 // }
2452 PropertyTy = llvm::StructType::get(Int8PtrTy,
2453 Int8PtrTy,
2454 NULL);
2455 CGM.getModule().addTypeName("struct._prop_t",
2456 PropertyTy);
2457
2458 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002459 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002460 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002461 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002462 // }
2463 PropertyListTy = llvm::StructType::get(IntTy,
2464 IntTy,
2465 llvm::ArrayType::get(PropertyTy, 0),
2466 NULL);
2467 CGM.getModule().addTypeName("struct._prop_list_t",
2468 PropertyListTy);
2469 // struct _prop_list_t *
2470 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2471
2472 // struct _objc_method {
2473 // SEL _cmd;
2474 // char *method_type;
2475 // char *_imp;
2476 // }
2477 MethodTy = llvm::StructType::get(SelectorPtrTy,
2478 Int8PtrTy,
2479 Int8PtrTy,
2480 NULL);
2481 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002482
2483 // struct _objc_cache *
2484 CacheTy = llvm::OpaqueType::get();
2485 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2486 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002487
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002488 // Property manipulation functions.
2489
2490 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2491 std::vector<const llvm::Type*> Params;
2492 Params.push_back(ObjectPtrTy);
2493 Params.push_back(SelectorPtrTy);
2494 Params.push_back(LongTy);
2495 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2496 GetPropertyFn =
2497 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2498 Params,
2499 false),
2500 "objc_getProperty");
2501
2502 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2503 Params.clear();
2504 Params.push_back(ObjectPtrTy);
2505 Params.push_back(SelectorPtrTy);
2506 Params.push_back(LongTy);
2507 Params.push_back(ObjectPtrTy);
2508 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2509 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2510 SetPropertyFn =
2511 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2512 Params,
2513 false),
2514 "objc_setProperty");
2515 // Enumeration mutation.
2516
2517 Params.clear();
2518 Params.push_back(ObjectPtrTy);
2519 EnumerationMutationFn =
2520 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2521 Params,
2522 false),
2523 "objc_enumerationMutation");
2524
2525 // gc's API
2526 // id objc_read_weak (id *)
2527 Params.clear();
2528 Params.push_back(PtrObjectPtrTy);
2529 GcReadWeakFn =
2530 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2531 Params,
2532 false),
2533 "objc_read_weak");
2534 // id objc_assign_weak (id, id *)
2535 Params.clear();
2536 Params.push_back(ObjectPtrTy);
2537 Params.push_back(PtrObjectPtrTy);
2538 GcAssignWeakFn =
2539 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2540 Params,
2541 false),
2542 "objc_assign_weak");
2543 GcAssignGlobalFn =
2544 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2545 Params,
2546 false),
2547 "objc_assign_global");
2548 GcAssignIvarFn =
2549 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2550 Params,
2551 false),
2552 "objc_assign_ivar");
2553 GcAssignStrongCastFn =
2554 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2555 Params,
2556 false),
2557 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002558}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002559
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002560ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2561 : ObjCCommonTypesHelper(cgm)
2562{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002563 // struct _objc_method_description {
2564 // SEL name;
2565 // char *types;
2566 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002567 MethodDescriptionTy =
2568 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002569 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002570 NULL);
2571 CGM.getModule().addTypeName("struct._objc_method_description",
2572 MethodDescriptionTy);
2573
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002574 // struct _objc_method_description_list {
2575 // int count;
2576 // struct _objc_method_description[1];
2577 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002578 MethodDescriptionListTy =
2579 llvm::StructType::get(IntTy,
2580 llvm::ArrayType::get(MethodDescriptionTy, 0),
2581 NULL);
2582 CGM.getModule().addTypeName("struct._objc_method_description_list",
2583 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002584
2585 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002586 MethodDescriptionListPtrTy =
2587 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2588
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002589 // Protocol description structures
2590
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002591 // struct _objc_protocol_extension {
2592 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2593 // struct _objc_method_description_list *optional_instance_methods;
2594 // struct _objc_method_description_list *optional_class_methods;
2595 // struct _objc_property_list *instance_properties;
2596 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002597 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002598 llvm::StructType::get(IntTy,
2599 MethodDescriptionListPtrTy,
2600 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002601 PropertyListPtrTy,
2602 NULL);
2603 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2604 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002605
2606 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002607 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2608
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002609 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002610
2611 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2612 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2613
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002614 const llvm::Type *T =
2615 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2616 LongTy,
2617 llvm::ArrayType::get(ProtocolTyHolder, 0),
2618 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002619 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2620
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002621 // struct _objc_protocol {
2622 // struct _objc_protocol_extension *isa;
2623 // char *protocol_name;
2624 // struct _objc_protocol **_objc_protocol_list;
2625 // struct _objc_method_description_list *instance_methods;
2626 // struct _objc_method_description_list *class_methods;
2627 // }
2628 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002629 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002630 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2631 MethodDescriptionListPtrTy,
2632 MethodDescriptionListPtrTy,
2633 NULL);
2634 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2635
2636 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2637 CGM.getModule().addTypeName("struct._objc_protocol_list",
2638 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002639 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002640 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2641
2642 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002643 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002644 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002645
2646 // Class description structures
2647
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002648 // struct _objc_ivar {
2649 // char *ivar_name;
2650 // char *ivar_type;
2651 // int ivar_offset;
2652 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002653 IvarTy = llvm::StructType::get(Int8PtrTy,
2654 Int8PtrTy,
2655 IntTy,
2656 NULL);
2657 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2658
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002659 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002660 IvarListTy = llvm::OpaqueType::get();
2661 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2662 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2663
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002664 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002665 MethodListTy = llvm::OpaqueType::get();
2666 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2667 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2668
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002669 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002670 ClassExtensionTy =
2671 llvm::StructType::get(IntTy,
2672 Int8PtrTy,
2673 PropertyListPtrTy,
2674 NULL);
2675 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2676 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2677
2678 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2679
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002680 // struct _objc_class {
2681 // Class isa;
2682 // Class super_class;
2683 // char *name;
2684 // long version;
2685 // long info;
2686 // long instance_size;
2687 // struct _objc_ivar_list *ivars;
2688 // struct _objc_method_list *methods;
2689 // struct _objc_cache *cache;
2690 // struct _objc_protocol_list *protocols;
2691 // char *ivar_layout;
2692 // struct _objc_class_ext *ext;
2693 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002694 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2695 llvm::PointerType::getUnqual(ClassTyHolder),
2696 Int8PtrTy,
2697 LongTy,
2698 LongTy,
2699 LongTy,
2700 IvarListPtrTy,
2701 MethodListPtrTy,
2702 CachePtrTy,
2703 ProtocolListPtrTy,
2704 Int8PtrTy,
2705 ClassExtensionPtrTy,
2706 NULL);
2707 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2708
2709 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2710 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2711 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2712
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002713 // struct _objc_category {
2714 // char *category_name;
2715 // char *class_name;
2716 // struct _objc_method_list *instance_method;
2717 // struct _objc_method_list *class_method;
2718 // uint32_t size; // sizeof(struct _objc_category)
2719 // struct _objc_property_list *instance_properties;// category's @property
2720 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002721 CategoryTy = llvm::StructType::get(Int8PtrTy,
2722 Int8PtrTy,
2723 MethodListPtrTy,
2724 MethodListPtrTy,
2725 ProtocolListPtrTy,
2726 IntTy,
2727 PropertyListPtrTy,
2728 NULL);
2729 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2730
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002731 // Global metadata structures
2732
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002733 // struct _objc_symtab {
2734 // long sel_ref_cnt;
2735 // SEL *refs;
2736 // short cls_def_cnt;
2737 // short cat_def_cnt;
2738 // char *defs[cls_def_cnt + cat_def_cnt];
2739 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002740 SymtabTy = llvm::StructType::get(LongTy,
2741 SelectorPtrTy,
2742 ShortTy,
2743 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002744 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002745 NULL);
2746 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2747 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2748
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002749 // struct _objc_module {
2750 // long version;
2751 // long size; // sizeof(struct _objc_module)
2752 // char *name;
2753 // struct _objc_symtab* symtab;
2754 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002755 ModuleTy =
2756 llvm::StructType::get(LongTy,
2757 LongTy,
2758 Int8PtrTy,
2759 SymtabPtrTy,
2760 NULL);
2761 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002762
Daniel Dunbar49f66022008-09-24 03:38:44 +00002763 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002764
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002765 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002766 std::vector<const llvm::Type*> Params;
2767 Params.push_back(ObjectPtrTy);
2768 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002769 MessageSendFn =
2770 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2771 Params,
2772 true),
2773 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002774
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002775 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002776 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002777 Params.push_back(ObjectPtrTy);
2778 Params.push_back(SelectorPtrTy);
2779 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002780 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2781 Params,
2782 true),
2783 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002784
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002785 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002786 Params.clear();
2787 Params.push_back(ObjectPtrTy);
2788 Params.push_back(SelectorPtrTy);
2789 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002790 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002791 MessageSendFpretFn =
2792 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2793 Params,
2794 true),
2795 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002796
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002797 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002798 Params.clear();
2799 Params.push_back(SuperPtrTy);
2800 Params.push_back(SelectorPtrTy);
2801 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002802 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2803 Params,
2804 true),
2805 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002806
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002807 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2808 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002809 Params.clear();
2810 Params.push_back(Int8PtrTy);
2811 Params.push_back(SuperPtrTy);
2812 Params.push_back(SelectorPtrTy);
2813 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002814 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2815 Params,
2816 true),
2817 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002818
2819 // There is no objc_msgSendSuper_fpret? How can that work?
2820 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002821
Anders Carlsson124526b2008-09-09 10:10:21 +00002822 // FIXME: This is the size of the setjmp buffer and should be
2823 // target specific. 18 is what's used on 32-bit X86.
2824 uint64_t SetJmpBufferSize = 18;
2825
2826 // Exceptions
2827 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002828 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002829
2830 ExceptionDataTy =
2831 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2832 SetJmpBufferSize),
2833 StackPtrTy, NULL);
2834 CGM.getModule().addTypeName("struct._objc_exception_data",
2835 ExceptionDataTy);
2836
2837 Params.clear();
2838 Params.push_back(ObjectPtrTy);
2839 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002840 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2841 Params,
2842 false),
2843 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002844
2845 Params.clear();
2846 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2847 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002848 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2849 Params,
2850 false),
2851 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002852 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002853 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2854 Params,
2855 false),
2856 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002857 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002858 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2859 Params,
2860 false),
2861 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002862
2863 Params.clear();
2864 Params.push_back(ClassPtrTy);
2865 Params.push_back(ObjectPtrTy);
2866 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002867 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2868 Params,
2869 false),
2870 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002871
2872 // synchronized APIs
2873 // void objc_sync_enter (id)
2874 Params.clear();
2875 Params.push_back(ObjectPtrTy);
2876 SyncEnterFn =
2877 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2878 Params,
2879 false),
2880 "objc_sync_enter");
2881 // void objc_sync_exit (id)
2882 SyncExitFn =
2883 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2884 Params,
2885 false),
2886 "objc_sync_exit");
2887
Anders Carlsson124526b2008-09-09 10:10:21 +00002888
2889 Params.clear();
2890 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2891 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002892 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2893 Params,
2894 false),
2895 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002896
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002897}
2898
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002899ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002900: ObjCCommonTypesHelper(cgm)
2901{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002902 // struct _method_list_t {
2903 // uint32_t entsize; // sizeof(struct _objc_method)
2904 // uint32_t method_count;
2905 // struct _objc_method method_list[method_count];
2906 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002907 MethodListnfABITy = llvm::StructType::get(IntTy,
2908 IntTy,
2909 llvm::ArrayType::get(MethodTy, 0),
2910 NULL);
2911 CGM.getModule().addTypeName("struct.__method_list_t",
2912 MethodListnfABITy);
2913 // struct method_list_t *
2914 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002915
2916 // struct _protocol_t {
2917 // id isa; // NULL
2918 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002919 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002920 // const struct method_list_t * const instance_methods;
2921 // const struct method_list_t * const class_methods;
2922 // const struct method_list_t *optionalInstanceMethods;
2923 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002924 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002925 // const uint32_t size; // sizeof(struct _protocol_t)
2926 // const uint32_t flags; // = 0
2927 // }
2928
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002929 // Holder for struct _protocol_list_t *
2930 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2931
2932 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2933 Int8PtrTy,
2934 llvm::PointerType::getUnqual(
2935 ProtocolListTyHolder),
2936 MethodListnfABIPtrTy,
2937 MethodListnfABIPtrTy,
2938 MethodListnfABIPtrTy,
2939 MethodListnfABIPtrTy,
2940 PropertyListPtrTy,
2941 IntTy,
2942 IntTy,
2943 NULL);
2944 CGM.getModule().addTypeName("struct._protocol_t",
2945 ProtocolnfABITy);
2946
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002947 // struct _objc_protocol_list {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002948 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002949 // struct _protocol_t[protocol_count];
2950 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002951 ProtocolListnfABITy = llvm::StructType::get(LongTy,
2952 llvm::ArrayType::get(
2953 ProtocolnfABITy, 0),
2954 NULL);
2955 CGM.getModule().addTypeName("struct._objc_protocol_list",
2956 ProtocolListnfABITy);
2957
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002958 // struct _objc_protocol_list*
2959 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002960
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002961 // FIXME! Is this doing the right thing?
2962 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
2963 ProtocolListnfABIPtrTy);
2964
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002965 // struct _ivar_t {
2966 // unsigned long int *offset; // pointer to ivar offset location
2967 // char *name;
2968 // char *type;
2969 // uint32_t alignment;
2970 // uint32_t size;
2971 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002972 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
2973 Int8PtrTy,
2974 Int8PtrTy,
2975 IntTy,
2976 IntTy,
2977 NULL);
2978 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
2979
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002980 // struct _ivar_list_t {
2981 // uint32 entsize; // sizeof(struct _ivar_t)
2982 // uint32 count;
2983 // struct _iver_t list[count];
2984 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002985 IvarListnfABITy = llvm::StructType::get(IntTy,
2986 IntTy,
2987 llvm::ArrayType::get(
2988 IvarnfABITy, 0),
2989 NULL);
2990 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
2991
2992 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002993
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002994 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002995 // uint32_t const flags;
2996 // uint32_t const instanceStart;
2997 // uint32_t const instanceSize;
2998 // uint32_t const reserved; // only when building for 64bit targets
2999 // const uint8_t * const ivarLayout;
3000 // const char *const name;
3001 // const struct _method_list_t * const baseMethods;
3002 // const struct _objc_protocol_list *const baseProtocols;
3003 // const struct _ivar_list_t *const ivars;
3004 // const uint8_t * const weakIvarLayout;
3005 // const struct _prop_list_t * const properties;
3006 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003007
3008 // FIXME. Add 'reserved' field in 64bit abi mode!
3009 ClassRonfABITy = llvm::StructType::get(IntTy,
3010 IntTy,
3011 IntTy,
3012 Int8PtrTy,
3013 Int8PtrTy,
3014 MethodListnfABIPtrTy,
3015 ProtocolListnfABIPtrTy,
3016 IvarListnfABIPtrTy,
3017 Int8PtrTy,
3018 PropertyListPtrTy,
3019 NULL);
3020 CGM.getModule().addTypeName("struct._class_ro_t",
3021 ClassRonfABITy);
3022
3023 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3024 std::vector<const llvm::Type*> Params;
3025 Params.push_back(ObjectPtrTy);
3026 Params.push_back(SelectorPtrTy);
3027 ImpnfABITy = llvm::PointerType::getUnqual(
3028 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3029
3030 // struct _class_t {
3031 // struct _class_t *isa;
3032 // struct _class_t * const superclass;
3033 // void *cache;
3034 // IMP *vtable;
3035 // struct class_ro_t *ro;
3036 // }
3037
3038 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3039 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3040 llvm::PointerType::getUnqual(ClassTyHolder),
3041 CachePtrTy,
3042 llvm::PointerType::getUnqual(ImpnfABITy),
3043 llvm::PointerType::getUnqual(
3044 ClassRonfABITy),
3045 NULL);
3046 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3047
3048 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3049 ClassnfABITy);
3050
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003051 // LLVM for struct _class_t *
3052 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3053
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003054 // struct _category_t {
3055 // const char * const name;
3056 // struct _class_t *const cls;
3057 // const struct _method_list_t * const instance_methods;
3058 // const struct _method_list_t * const class_methods;
3059 // const struct _protocol_list_t * const protocols;
3060 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003061 // }
3062 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003063 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003064 MethodListnfABIPtrTy,
3065 MethodListnfABIPtrTy,
3066 ProtocolListnfABIPtrTy,
3067 PropertyListPtrTy,
3068 NULL);
3069 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3070
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003071}
3072
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003073llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3074 FinishNonFragileABIModule();
3075
3076 return NULL;
3077}
3078
3079void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3080 // nonfragile abi has no module definition.
3081 std::vector<llvm::Constant*> Used;
3082 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3083 e = UsedGlobals.end(); i != e; ++i) {
3084 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3085 }
3086
3087 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3088 llvm::GlobalValue *GV =
3089 new llvm::GlobalVariable(AT, false,
3090 llvm::GlobalValue::AppendingLinkage,
3091 llvm::ConstantArray::get(AT, Used),
3092 "llvm.used",
3093 &CGM.getModule());
3094
3095 GV->setSection("llvm.metadata");
3096
3097}
3098
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003099// Metadata flags
3100enum MetaDataDlags {
3101 CLS = 0x0,
3102 CLS_META = 0x1,
3103 CLS_ROOT = 0x2,
3104 CLS_HIDDEN = 0x10,
3105 CLS_EXCEPTION = 0x20
3106};
3107/// BuildClassRoTInitializer - generate meta-data for:
3108/// struct _class_ro_t {
3109/// uint32_t const flags;
3110/// uint32_t const instanceStart;
3111/// uint32_t const instanceSize;
3112/// uint32_t const reserved; // only when building for 64bit targets
3113/// const uint8_t * const ivarLayout;
3114/// const char *const name;
3115/// const struct _method_list_t * const baseMethods;
3116/// const struct _objc_protocol_list *const baseProtocols;
3117/// const struct _ivar_list_t *const ivars;
3118/// const uint8_t * const weakIvarLayout;
3119/// const struct _prop_list_t * const properties;
3120/// }
3121///
3122llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3123 unsigned flags,
3124 unsigned InstanceStart,
3125 unsigned InstanceSize,
3126 const ObjCImplementationDecl *ID) {
3127 std::string ClassName = ID->getNameAsString();
3128 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3129 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3130 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3131 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3132 // FIXME. For 64bit targets add 0 here.
3133 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3134 Values[ 4] = GetClassName(ID->getIdentifier());
3135 Values[ 5] = llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3136 Values[ 6] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3137 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3138 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3139 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3140 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3141 Values);
3142 llvm::GlobalVariable *CLASS_RO_GV =
3143 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3144 llvm::GlobalValue::InternalLinkage,
3145 Init,
3146 (flags & CLS_META) ?
3147 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3148 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3149 &CGM.getModule());
3150 CLASS_RO_GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3151 UsedGlobals.push_back(CLASS_RO_GV);
3152 return CLASS_RO_GV;
3153}
3154
3155/// BuildClassMetaData - This routine defines that to-level meta-data
3156/// for the given ClassName for:
3157/// struct _class_t {
3158/// struct _class_t *isa;
3159/// struct _class_t * const superclass;
3160/// void *cache;
3161/// IMP *vtable;
3162/// struct class_ro_t *ro;
3163/// }
3164///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003165llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3166 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003167 llvm::Constant *IsAGV,
3168 llvm::Constant *SuperClassGV,
3169 llvm::Constant *ClassRoGV) {
3170 std::vector<llvm::Constant*> Values(5);
3171 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003172 Values[1] = SuperClassGV
3173 ? SuperClassGV
3174 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003175 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3176 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3177 Values[4] = ClassRoGV; // &CLASS_RO_GV
3178 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3179 Values);
3180 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3181 if (GV)
3182 GV->setInitializer(Init);
3183 else
3184 GV =
3185 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3186 llvm::GlobalValue::ExternalLinkage,
3187 Init,
3188 ClassName,
3189 &CGM.getModule());
3190 GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3191 UsedGlobals.push_back(GV);
3192 // FIXME! why?
3193 GV->setAlignment(32);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003194 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003195}
3196
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003197void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3198 std::string ClassName = ID->getNameAsString();
3199 if (!ObjCEmptyCacheVar) {
3200 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3201 ObjCTypes.CachePtrTy,
3202 false,
3203 llvm::GlobalValue::ExternalLinkage,
3204 0,
3205 "\01_objc_empty_cache",
3206 &CGM.getModule());
3207 UsedGlobals.push_back(ObjCEmptyCacheVar);
3208
3209 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3210 llvm::PointerType::getUnqual(
3211 ObjCTypes.ImpnfABITy),
3212 false,
3213 llvm::GlobalValue::ExternalLinkage,
3214 0,
3215 "\01_objc_empty_vtable",
3216 &CGM.getModule());
3217 UsedGlobals.push_back(ObjCEmptyVtableVar);
3218 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003219 uint32_t InstanceStart =
3220 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3221 uint32_t InstanceSize = InstanceStart;
3222 uint32_t flags = CLS_META;
3223 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3224 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003225
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003226 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003227
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003228 if (ID->getClassInterface() && !ID->getClassInterface()->getSuperClass()) {
3229 // class is root
3230 flags |= CLS_ROOT;
3231 std::string SuperClassName = ObjCClassName + ClassName;
3232 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3233 if (!SuperClassGV)
3234 SuperClassGV =
3235 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3236 llvm::GlobalValue::ExternalLinkage,
3237 0,
3238 SuperClassName,
3239 &CGM.getModule());
3240 UsedGlobals.push_back(SuperClassGV);
3241 std::string IsAClassName = ObjCMetaClassName + ClassName;
3242 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3243 if (!IsAGV)
3244 IsAGV =
3245 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3246 llvm::GlobalValue::ExternalLinkage,
3247 0,
3248 IsAClassName,
3249 &CGM.getModule());
3250 UsedGlobals.push_back(IsAGV);
3251 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003252 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003253 std::string RootClassName =
3254 ID->getClassInterface()->getSuperClass()->getNameAsString();
3255 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3256 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3257 if (!SuperClassGV)
3258 SuperClassGV =
3259 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3260 llvm::GlobalValue::ExternalLinkage,
3261 0,
3262 SuperClassName,
3263 &CGM.getModule());
3264 UsedGlobals.push_back(SuperClassGV);
3265 IsAGV = SuperClassGV;
3266 }
3267 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3268 InstanceStart,
3269 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003270 std::string TClassName = ObjCMetaClassName + ClassName;
3271 llvm::GlobalVariable *MetaTClass =
3272 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3273
3274 // Metadata for the class
3275 flags = CLS;
3276 if (ID->getClassInterface() && !ID->getClassInterface()->getSuperClass()) {
3277 flags |= CLS_ROOT;
3278 SuperClassGV = 0;
3279 }
3280 else {
3281 // Has a root. Current class is not a root.
3282 std::string RootClassName =
3283 ID->getClassInterface()->getSuperClass()->getNameAsString();
3284 std::string SuperClassName = ObjCClassName + RootClassName;
3285 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3286 if (!SuperClassGV)
3287 SuperClassGV =
3288 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3289 llvm::GlobalValue::ExternalLinkage,
3290 0,
3291 SuperClassName,
3292 &CGM.getModule());
3293 UsedGlobals.push_back(SuperClassGV);
3294
3295 }
3296
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003297 InstanceStart = InstanceSize = 0;
3298 if (ObjCInterfaceDecl *OID =
3299 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3300 // FIXME. Share this with the one in EmitIvarList.
3301 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
3302 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
3303 RecordDecl::field_iterator firstField = RD->field_begin();
3304 RecordDecl::field_iterator lastField = RD->field_end();
3305
3306 while (countSuperClassIvars-- > 0) {
3307 lastField = firstField;
3308 ++firstField;
3309 }
3310
3311 for (RecordDecl::field_iterator e = RD->field_end(),
3312 ifield = firstField; ifield != e; ++ifield)
3313 lastField = ifield;
3314
3315 const llvm::Type *InterfaceTy =
3316 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3317 const llvm::StructLayout *Layout =
3318 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
3319
3320 if (lastField != RD->field_end()) {
3321 FieldDecl *Field = *lastField;
3322 const llvm::Type *FieldTy =
3323 CGM.getTypes().ConvertTypeForMem(Field->getType());
3324 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3325 InstanceSize = Layout->getElementOffset(
3326 CGM.getTypes().getLLVMFieldNo(Field)) +
3327 Size;
3328 if (firstField == RD->field_end())
3329 InstanceStart = InstanceSize;
3330 else
3331 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3332 getLLVMFieldNo(*firstField));
3333 }
3334 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003335 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003336 InstanceStart,
3337 InstanceSize,
3338 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003339
3340 TClassName = ObjCClassName + ClassName;
3341 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003342}
3343
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003344/* *** */
3345
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003346CodeGen::CGObjCRuntime *
3347CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003348 return new CGObjCMac(CGM);
3349}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003350
3351CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003352CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003353 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003354}