blob: a1d6d47bd54bf434435f9631f3f36e8f42a06601 [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
68 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
69 ~ObjCCommonTypesHelper(){}
70};
Daniel Dunbare8b470d2008-08-23 04:28:29 +000071
Fariborz Jahanianee0af742009-01-21 22:04:16 +000072/// ObjCTypesHelper - Helper class that encapsulates lazy
73/// construction of varies types used during ObjC generation.
74class ObjCTypesHelper : public ObjCCommonTypesHelper {
75private:
76
77 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
78 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
79 *MessageSendSuperFpretFn;
80
81public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000082 /// SymtabTy - LLVM type for struct objc_symtab.
83 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000084 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
85 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000086 /// ModuleTy - LLVM type for struct objc_module.
87 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000088
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000089 /// ProtocolTy - LLVM type for struct objc_protocol.
90 const llvm::StructType *ProtocolTy;
91 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
92 const llvm::Type *ProtocolPtrTy;
93 /// ProtocolExtensionTy - LLVM type for struct
94 /// objc_protocol_extension.
95 const llvm::StructType *ProtocolExtensionTy;
96 /// ProtocolExtensionTy - LLVM type for struct
97 /// objc_protocol_extension *.
98 const llvm::Type *ProtocolExtensionPtrTy;
99 /// MethodDescriptionTy - LLVM type for struct
100 /// objc_method_description.
101 const llvm::StructType *MethodDescriptionTy;
102 /// MethodDescriptionListTy - LLVM type for struct
103 /// objc_method_description_list.
104 const llvm::StructType *MethodDescriptionListTy;
105 /// MethodDescriptionListPtrTy - LLVM type for struct
106 /// objc_method_description_list *.
107 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000108 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
109 /// in GCC parlance).
110 const llvm::StructType *PropertyTy;
111 /// PropertyListTy - LLVM type for struct objc_property_list
112 /// (_prop_list_t in GCC parlance).
113 const llvm::StructType *PropertyListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000114 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
115 const llvm::Type *PropertyListPtrTy;
116 /// ProtocolListTy - LLVM type for struct objc_property_list.
117 const llvm::Type *ProtocolListTy;
118 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
119 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000120 /// CategoryTy - LLVM type for struct objc_category.
121 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000122 /// ClassTy - LLVM type for struct objc_class.
123 const llvm::StructType *ClassTy;
124 /// ClassPtrTy - LLVM type for struct objc_class *.
125 const llvm::Type *ClassPtrTy;
126 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
127 const llvm::StructType *ClassExtensionTy;
128 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
129 const llvm::Type *ClassExtensionPtrTy;
130 /// CacheTy - LLVM type for struct objc_cache.
131 const llvm::Type *CacheTy;
132 /// CachePtrTy - LLVM type for struct objc_cache *.
133 const llvm::Type *CachePtrTy;
134 // IvarTy - LLVM type for struct objc_ivar.
135 const llvm::StructType *IvarTy;
136 /// IvarListTy - LLVM type for struct objc_ivar_list.
137 const llvm::Type *IvarListTy;
138 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
139 const llvm::Type *IvarListPtrTy;
140 // MethodTy - LLVM type for struct objc_method.
141 const llvm::StructType *MethodTy;
142 /// MethodListTy - LLVM type for struct objc_method_list.
143 const llvm::Type *MethodListTy;
144 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
145 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000146
Daniel Dunbar49f66022008-09-24 03:38:44 +0000147 llvm::Function *GetPropertyFn, *SetPropertyFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000148 llvm::Function *EnumerationMutationFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000149
150 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
151 const llvm::Type *ExceptionDataTy;
152
153 /// ExceptionThrowFn - LLVM objc_exception_throw function.
154 llvm::Function *ExceptionThrowFn;
155
156 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
157 llvm::Function *ExceptionTryEnterFn;
158
159 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
160 llvm::Function *ExceptionTryExitFn;
161
162 /// ExceptionExtractFn - LLVM objc_exception_extract function.
163 llvm::Function *ExceptionExtractFn;
164
165 /// ExceptionMatchFn - LLVM objc_exception_match function.
166 llvm::Function *ExceptionMatchFn;
167
168 /// SetJmpFn - LLVM _setjmp function.
169 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000170
171 /// SyncEnterFn - LLVM object_sync_enter function.
172 llvm::Function *SyncEnterFn;
173
174 /// SyncExitFn - LLVM object_sync_exit function.
175 llvm::Function *SyncExitFn;
176
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000177 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
178 llvm::Function *GcReadWeakFn;
179
180 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
181 llvm::Function *GcAssignWeakFn;
182
183 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
184 llvm::Function *GcAssignGlobalFn;
185
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000186 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
187 llvm::Function *GcAssignIvarFn;
188
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000189 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
190 llvm::Function *GcAssignStrongCastFn;
191
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000192public:
193 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000194 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000195
196
197 llvm::Function *getSendFn(bool IsSuper) {
198 return IsSuper ? MessageSendSuperFn : MessageSendFn;
199 }
200
201 llvm::Function *getSendStretFn(bool IsSuper) {
202 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
203 }
204
205 llvm::Function *getSendFpretFn(bool IsSuper) {
206 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
207 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000208};
209
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000210/// ObjCModernTypesHelper - will have all types needed by objective-c's
211/// modern abi
212class ObjCModernTypesHelper : public ObjCCommonTypesHelper {
213public:
214 ObjCModernTypesHelper(CodeGen::CodeGenModule &cgm);
215 ~ObjCModernTypesHelper(){}
216};
217
218class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
219protected:
220 CodeGen::CodeGenModule &CGM;
221 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000222 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000223
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000224 /// LazySymbols - Symbols to generate a lazy reference for. See
225 /// DefinedSymbols and FinishModule().
226 std::set<IdentifierInfo*> LazySymbols;
227
228 /// DefinedSymbols - External symbols which are defined by this
229 /// module. The symbols in this list and LazySymbols are used to add
230 /// special linker symbols which ensure that Objective-C modules are
231 /// linked properly.
232 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000233
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000234 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000235 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000236
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000237 /// MethodVarNames - uniqued method variable names.
238 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000239
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000240 /// MethodVarTypes - uniqued method type signatures. We have to use
241 /// a StringMap here because have no other unique reference.
242 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000243
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000244 /// MethodDefinitions - map of methods which have been defined in
245 /// this translation unit.
246 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000247
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000248 /// PropertyNames - uniqued method variable names.
249 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000250
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000251 /// ClassReferences - uniqued class references.
252 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000253
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000254 /// SelectorReferences - uniqued selector references.
255 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000256
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000257 /// Protocols - Protocols for which an objc_protocol structure has
258 /// been emitted. Forward declarations are handled by creating an
259 /// empty structure whose initializer is filled in when/if defined.
260 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000261
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000262 /// DefinedProtocols - Protocols which have actually been
263 /// defined. We should not need this, see FIXME in GenerateProtocol.
264 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000265
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000266 /// DefinedClasses - List of defined classes.
267 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000268
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000269 /// DefinedCategories - List of defined categories.
270 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000271
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000272 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000273 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000274 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000275
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000276 /// GetNameForMethod - Return a name for the given method.
277 /// \param[out] NameOut - The return value.
278 void GetNameForMethod(const ObjCMethodDecl *OMD,
279 const ObjCContainerDecl *CD,
280 std::string &NameOut);
281
282 /// GetMethodVarName - Return a unique constant for the given
283 /// selector's name. The return value has type char *.
284 llvm::Constant *GetMethodVarName(Selector Sel);
285 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
286 llvm::Constant *GetMethodVarName(const std::string &Name);
287
288 /// GetMethodVarType - Return a unique constant for the given
289 /// selector's name. The return value has type char *.
290
291 // FIXME: This is a horrible name.
292 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
293 llvm::Constant *GetMethodVarType(const std::string &Name);
294
295 /// GetPropertyName - Return a unique constant for the given
296 /// name. The return value has type char *.
297 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
298
299 // FIXME: This can be dropped once string functions are unified.
300 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
301 const Decl *Container);
302
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000303public:
304 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
305 { }
306};
307
308class CGObjCMac : public CGObjCCommonMac {
309private:
310 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000311 /// EmitImageInfo - Emit the image info marker used to encode some module
312 /// level information.
313 void EmitImageInfo();
314
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000315 /// EmitModuleInfo - Another marker encoding module level
316 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000317 void EmitModuleInfo();
318
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000319 /// EmitModuleSymols - Emit module symbols, the list of defined
320 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000321 llvm::Constant *EmitModuleSymbols();
322
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000323 /// FinishModule - Write out global data structures at the end of
324 /// processing a translation unit.
325 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000326
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000327 /// EmitClassExtension - Generate the class extension structure used
328 /// to store the weak ivar layout and properties. The return value
329 /// has type ClassExtensionPtrTy.
330 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
331
332 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
333 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000334 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000335 const ObjCInterfaceDecl *ID);
336
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000337 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000338 QualType ResultType,
339 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000340 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000341 QualType Arg0Ty,
342 bool IsSuper,
343 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000344
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000345 /// EmitIvarList - Emit the ivar list for the given
346 /// implementation. If ForClass is true the list of class ivars
347 /// (i.e. metaclass ivars) is emitted, otherwise the list of
348 /// interface ivars will be emitted. The return value has type
349 /// IvarListPtrTy.
350 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
351 bool ForClass,
352 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000353
354 /// EmitMetaClass - Emit a forward reference to the class structure
355 /// for the metaclass of the given interface. The return value has
356 /// type ClassPtrTy.
357 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
358
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000359 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000360 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000361 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
362 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000363 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000364 const ConstantVector &Methods);
365
366 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
367
368 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000369
370 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000371 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000372 llvm::Constant *EmitMethodList(const std::string &Name,
373 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000374 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000375
376 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000377 /// method declarations.
378 /// - TypeName: The name for the type containing the methods.
379 /// - IsProtocol: True iff these methods are for a protocol.
380 /// - ClassMethds: True iff these are class methods.
381 /// - Required: When true, only "required" methods are
382 /// listed. Similarly, when false only "optional" methods are
383 /// listed. For classes this should always be true.
384 /// - begin, end: The method list to output.
385 ///
386 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000387 llvm::Constant *EmitMethodDescList(const std::string &Name,
388 const char *Section,
389 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000390
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000391 /// EmitPropertyList - Emit the given property list. The return
392 /// value has type PropertyListPtrTy.
393 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000394 const Decl *Container,
395 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000396
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000397 /// GetOrEmitProtocol - Get the protocol object for the given
398 /// declaration, emitting it if necessary. The return value has type
399 /// ProtocolPtrTy.
400 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
401
402 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
403 /// object for the given declaration, emitting it if needed. These
404 /// forward references will be filled in with empty bodies if no
405 /// definition is seen. The return value has type ProtocolPtrTy.
406 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
407
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000408 /// EmitProtocolExtension - Generate the protocol extension
409 /// structure used to store optional instance and class methods, and
410 /// protocol properties. The return value has type
411 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000412 llvm::Constant *
413 EmitProtocolExtension(const ObjCProtocolDecl *PD,
414 const ConstantVector &OptInstanceMethods,
415 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000416
417 /// EmitProtocolList - Generate the list of referenced
418 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000419 llvm::Constant *EmitProtocolList(const std::string &Name,
420 ObjCProtocolDecl::protocol_iterator begin,
421 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000422
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000423 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
424 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000425 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000427 /// GetProtocolRef - Return a reference to the internal protocol
428 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000429 /// defined. The return value has type ProtocolPtrTy.
430 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000431
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000432 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000433 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000434 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000435
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000436public:
437 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000438 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000439
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000440 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000441 QualType ResultType,
442 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000443 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000444 bool IsClassMessage,
445 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000446
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000447 virtual CodeGen::RValue
448 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000449 QualType ResultType,
450 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000451 const ObjCInterfaceDecl *Class,
452 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000453 bool IsClassMessage,
454 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000455
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000456 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000457 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000458
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000459 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000460
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000461 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
462 const ObjCContainerDecl *CD=0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000463
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000464 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000465
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000466 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000467
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000468 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000469 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000470
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000471 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000472
473 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000474 virtual llvm::Function *GetPropertyGetFunction();
475 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000476 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000477
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000478 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
479 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000480 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
481 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000482 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000483 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000484 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
485 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000486 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
487 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000488 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
489 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000490 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
491 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000492};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000493
494class CGObjCModernMac : public CGObjCCommonMac {
495private:
496 ObjCModernTypesHelper ObjCTypes;
497public:
498 CGObjCModernMac(CodeGen::CodeGenModule &cgm);
499};
500
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000501} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000502
503/* *** Helper Functions *** */
504
505/// getConstantGEP() - Help routine to construct simple GEPs.
506static llvm::Constant *getConstantGEP(llvm::Constant *C,
507 unsigned idx0,
508 unsigned idx1) {
509 llvm::Value *Idxs[] = {
510 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
511 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
512 };
513 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
514}
515
516/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000517
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000518CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
519 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000520{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000521 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000522 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000523}
524
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000525/// GetClass - Return a reference to the class for the given interface
526/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000527llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 const ObjCInterfaceDecl *ID) {
529 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530}
531
532/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000533llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000534 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000535}
536
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000537/// Generate a constant CFString object.
538/*
539 struct __builtin_CFString {
540 const int *isa; // point to __CFConstantStringClassReference
541 int flags;
542 const char *str;
543 long length;
544 };
545*/
546
547llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000548 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000549}
550
551/// Generates a message send where the super is the receiver. This is
552/// a message send to self with special delivery semantics indicating
553/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000554CodeGen::RValue
555CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000556 QualType ResultType,
557 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000558 const ObjCInterfaceDecl *Class,
559 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000560 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000561 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000562 // Create and init a super structure; this is a (receiver, class)
563 // pair we will pass to objc_msgSendSuper.
564 llvm::Value *ObjCSuper =
565 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
566 llvm::Value *ReceiverAsObject =
567 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
568 CGF.Builder.CreateStore(ReceiverAsObject,
569 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000570
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000571 // If this is a class message the metaclass is passed as the target.
572 llvm::Value *Target;
573 if (IsClassMessage) {
574 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
575 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
576 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
577 Target = Super;
578 } else {
579 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
580 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000581 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
582 // and ObjCTypes types.
583 const llvm::Type *ClassTy =
584 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000585 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000586 CGF.Builder.CreateStore(Target,
587 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
588
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000589 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000590 ObjCSuper, ObjCTypes.SuperPtrCTy,
591 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000592}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000593
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000594/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000595CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000596 QualType ResultType,
597 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000598 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000599 bool IsClassMessage,
600 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000601 llvm::Value *Arg0 =
602 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000603 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000604 Arg0, CGF.getContext().getObjCIdType(),
605 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000606}
607
608CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000609 QualType ResultType,
610 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000611 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000612 QualType Arg0Ty,
613 bool IsSuper,
614 const CallArgList &CallArgs) {
615 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000616 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
617 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
618 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000619 CGF.getContext().getObjCSelType()));
620 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000621
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000622 const llvm::FunctionType *FTy =
623 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
624 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000625
626 llvm::Constant *Fn;
627 if (CGM.ReturnTypeUsesSret(ResultType)) {
628 Fn = ObjCTypes.getSendStretFn(IsSuper);
629 } else if (ResultType->isFloatingType()) {
630 // FIXME: Sadly, this is wrong. This actually depends on the
631 // architecture. This happens to be right for x86-32 though.
632 Fn = ObjCTypes.getSendFpretFn(IsSuper);
633 } else {
634 Fn = ObjCTypes.getSendFn(IsSuper);
635 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000636 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000637 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000638}
639
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000640llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000641 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000642 // FIXME: I don't understand why gcc generates this, or where it is
643 // resolved. Investigate. Its also wasteful to look this up over and
644 // over.
645 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
646
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000647 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
648 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000649}
650
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000651void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
652 // FIXME: We shouldn't need this, the protocol decl should contain
653 // enough information to tell us whether this was a declaration or a
654 // definition.
655 DefinedProtocols.insert(PD->getIdentifier());
656
657 // If we have generated a forward reference to this protocol, emit
658 // it now. Otherwise do nothing, the protocol objects are lazily
659 // emitted.
660 if (Protocols.count(PD->getIdentifier()))
661 GetOrEmitProtocol(PD);
662}
663
664llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
665 if (DefinedProtocols.count(PD->getIdentifier()))
666 return GetOrEmitProtocol(PD);
667 return GetOrEmitProtocolRef(PD);
668}
669
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000670/*
671 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
672 struct _objc_protocol {
673 struct _objc_protocol_extension *isa;
674 char *protocol_name;
675 struct _objc_protocol_list *protocol_list;
676 struct _objc__method_prototype_list *instance_methods;
677 struct _objc__method_prototype_list *class_methods
678 };
679
680 See EmitProtocolExtension().
681*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000682llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
683 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
684
685 // Early exit if a defining object has already been generated.
686 if (Entry && Entry->hasInitializer())
687 return Entry;
688
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000689 // FIXME: I don't understand why gcc generates this, or where it is
690 // resolved. Investigate. Its also wasteful to look this up over and
691 // over.
692 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
693
Chris Lattner8ec03f52008-11-24 03:54:41 +0000694 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000695
696 // Construct method lists.
697 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
698 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
699 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
700 e = PD->instmeth_end(); i != e; ++i) {
701 ObjCMethodDecl *MD = *i;
702 llvm::Constant *C = GetMethodDescriptionConstant(MD);
703 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
704 OptInstanceMethods.push_back(C);
705 } else {
706 InstanceMethods.push_back(C);
707 }
708 }
709
710 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
711 e = PD->classmeth_end(); i != e; ++i) {
712 ObjCMethodDecl *MD = *i;
713 llvm::Constant *C = GetMethodDescriptionConstant(MD);
714 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
715 OptClassMethods.push_back(C);
716 } else {
717 ClassMethods.push_back(C);
718 }
719 }
720
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000721 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000722 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000723 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000724 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000725 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000726 PD->protocol_begin(),
727 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000728 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000729 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
730 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000731 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
732 InstanceMethods);
733 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000734 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
735 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000736 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
737 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000738 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
739 Values);
740
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000741 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000742 // Already created, fix the linkage and update the initializer.
743 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000744 Entry->setInitializer(Init);
745 } else {
746 Entry =
747 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
748 llvm::GlobalValue::InternalLinkage,
749 Init,
750 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
751 &CGM.getModule());
752 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
753 UsedGlobals.push_back(Entry);
754 // FIXME: Is this necessary? Why only for protocol?
755 Entry->setAlignment(4);
756 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000757
758 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000759}
760
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000761llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000762 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
763
764 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000765 // We use the initializer as a marker of whether this is a forward
766 // reference or not. At module finalization we add the empty
767 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000768 Entry =
769 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000770 llvm::GlobalValue::ExternalLinkage,
771 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000772 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000773 &CGM.getModule());
774 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
775 UsedGlobals.push_back(Entry);
776 // FIXME: Is this necessary? Why only for protocol?
777 Entry->setAlignment(4);
778 }
779
780 return Entry;
781}
782
783/*
784 struct _objc_protocol_extension {
785 uint32_t size;
786 struct objc_method_description_list *optional_instance_methods;
787 struct objc_method_description_list *optional_class_methods;
788 struct objc_property_list *instance_properties;
789 };
790*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000791llvm::Constant *
792CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
793 const ConstantVector &OptInstanceMethods,
794 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000795 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000796 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000797 std::vector<llvm::Constant*> Values(4);
798 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000799 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000800 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
801 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000802 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
803 OptInstanceMethods);
804 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000805 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
806 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000807 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
808 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000809 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
810 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000811 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000812
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000813 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000814 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
815 Values[3]->isNullValue())
816 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
817
818 llvm::Constant *Init =
819 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
820 llvm::GlobalVariable *GV =
821 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
822 llvm::GlobalValue::InternalLinkage,
823 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000824 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000825 &CGM.getModule());
826 // No special section, but goes in llvm.used
827 UsedGlobals.push_back(GV);
828
829 return GV;
830}
831
832/*
833 struct objc_protocol_list {
834 struct objc_protocol_list *next;
835 long count;
836 Protocol *list[];
837 };
838*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000839llvm::Constant *
840CGObjCMac::EmitProtocolList(const std::string &Name,
841 ObjCProtocolDecl::protocol_iterator begin,
842 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000843 std::vector<llvm::Constant*> ProtocolRefs;
844
Daniel Dunbardbc933702008-08-21 21:57:41 +0000845 for (; begin != end; ++begin)
846 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000847
848 // Just return null for empty protocol lists
849 if (ProtocolRefs.empty())
850 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
851
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000852 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000853 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
854
855 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000856 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000857 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
858 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
859 Values[2] =
860 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
861 ProtocolRefs.size()),
862 ProtocolRefs);
863
864 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
865 llvm::GlobalVariable *GV =
866 new llvm::GlobalVariable(Init->getType(), false,
867 llvm::GlobalValue::InternalLinkage,
868 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000869 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000870 &CGM.getModule());
871 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
872 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
873}
874
875/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000876 struct _objc_property {
877 const char * const name;
878 const char * const attributes;
879 };
880
881 struct _objc_property_list {
882 uint32_t entsize; // sizeof (struct _objc_property)
883 uint32_t prop_count;
884 struct _objc_property[prop_count];
885 };
886*/
887llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000888 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +0000889 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000890 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +0000891 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
892 E = OCD->prop_end(); I != E; ++I) {
893 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000894 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000895 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000896 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
897 Prop));
898 }
899
900 // Return null for empty list.
901 if (Properties.empty())
902 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
903
904 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000905 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000906 std::vector<llvm::Constant*> Values(3);
907 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
908 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
909 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
910 Properties.size());
911 Values[2] = llvm::ConstantArray::get(AT, Properties);
912 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
913
914 llvm::GlobalVariable *GV =
915 new llvm::GlobalVariable(Init->getType(), false,
916 llvm::GlobalValue::InternalLinkage,
917 Init,
918 Name,
919 &CGM.getModule());
920 // No special section on property lists?
921 UsedGlobals.push_back(GV);
922 return llvm::ConstantExpr::getBitCast(GV,
923 ObjCTypes.PropertyListPtrTy);
924
925}
926
927/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928 struct objc_method_description_list {
929 int count;
930 struct objc_method_description list[];
931 };
932*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000933llvm::Constant *
934CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
935 std::vector<llvm::Constant*> Desc(2);
936 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
937 ObjCTypes.SelectorPtrTy);
938 Desc[1] = GetMethodVarType(MD);
939 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
940 Desc);
941}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000942
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000943llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
944 const char *Section,
945 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000946 // Return null for empty list.
947 if (Methods.empty())
948 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
949
950 std::vector<llvm::Constant*> Values(2);
951 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
952 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
953 Methods.size());
954 Values[1] = llvm::ConstantArray::get(AT, Methods);
955 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
956
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000957 llvm::GlobalVariable *GV =
958 new llvm::GlobalVariable(Init->getType(), false,
959 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000960 Init, Name, &CGM.getModule());
961 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000962 UsedGlobals.push_back(GV);
963 return llvm::ConstantExpr::getBitCast(GV,
964 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000965}
966
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000967/*
968 struct _objc_category {
969 char *category_name;
970 char *class_name;
971 struct _objc_method_list *instance_methods;
972 struct _objc_method_list *class_methods;
973 struct _objc_protocol_list *protocols;
974 uint32_t size; // <rdar://4585769>
975 struct _objc_property_list *instance_properties;
976 };
977 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000978void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000979 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000980
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000981 // FIXME: This is poor design, the OCD should have a pointer to the
982 // category decl. Additionally, note that Category can be null for
983 // the @implementation w/o an @interface case. Sema should just
984 // create one for us as it does for @implementation so everyone else
985 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000986 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000987 const ObjCCategoryDecl *Category =
988 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000989 std::string ExtName(Interface->getNameAsString() + "_" +
990 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000991
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000992 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
993 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
994 e = OCD->instmeth_end(); i != e; ++i) {
995 // Instance methods should always be defined.
996 InstanceMethods.push_back(GetMethodConstant(*i));
997 }
998 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
999 e = OCD->classmeth_end(); i != e; ++i) {
1000 // Class methods should always be defined.
1001 ClassMethods.push_back(GetMethodConstant(*i));
1002 }
1003
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001004 std::vector<llvm::Constant*> Values(7);
1005 Values[0] = GetClassName(OCD->getIdentifier());
1006 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001007 Values[2] =
1008 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1009 ExtName,
1010 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001011 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001012 Values[3] =
1013 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1014 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001015 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001016 if (Category) {
1017 Values[4] =
1018 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1019 Category->protocol_begin(),
1020 Category->protocol_end());
1021 } else {
1022 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1023 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001024 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001025
1026 // If there is no category @interface then there can be no properties.
1027 if (Category) {
1028 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001029 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001030 } else {
1031 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1032 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001033
1034 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1035 Values);
1036
1037 llvm::GlobalVariable *GV =
1038 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1039 llvm::GlobalValue::InternalLinkage,
1040 Init,
1041 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1042 &CGM.getModule());
1043 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1044 UsedGlobals.push_back(GV);
1045 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001046}
1047
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001048// FIXME: Get from somewhere?
1049enum ClassFlags {
1050 eClassFlags_Factory = 0x00001,
1051 eClassFlags_Meta = 0x00002,
1052 // <rdr://5142207>
1053 eClassFlags_HasCXXStructors = 0x02000,
1054 eClassFlags_Hidden = 0x20000,
1055 eClassFlags_ABI2_Hidden = 0x00010,
1056 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1057};
1058
1059// <rdr://5142207&4705298&4843145>
1060static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1061 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1062 // FIXME: Support -fvisibility
1063 switch (attr->getVisibility()) {
1064 default:
1065 assert(0 && "Unknown visibility");
1066 return false;
1067 case VisibilityAttr::DefaultVisibility:
1068 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1069 return false;
1070 case VisibilityAttr::HiddenVisibility:
1071 return true;
1072 }
1073 } else {
1074 return false; // FIXME: Support -fvisibility
1075 }
1076}
1077
1078/*
1079 struct _objc_class {
1080 Class isa;
1081 Class super_class;
1082 const char *name;
1083 long version;
1084 long info;
1085 long instance_size;
1086 struct _objc_ivar_list *ivars;
1087 struct _objc_method_list *methods;
1088 struct _objc_cache *cache;
1089 struct _objc_protocol_list *protocols;
1090 // Objective-C 1.0 extensions (<rdr://4585769>)
1091 const char *ivar_layout;
1092 struct _objc_class_ext *ext;
1093 };
1094
1095 See EmitClassExtension();
1096 */
1097void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001098 DefinedSymbols.insert(ID->getIdentifier());
1099
Chris Lattner8ec03f52008-11-24 03:54:41 +00001100 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001101 // FIXME: Gross
1102 ObjCInterfaceDecl *Interface =
1103 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001104 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001105 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001106 Interface->protocol_begin(),
1107 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001108 const llvm::Type *InterfaceTy =
1109 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1110 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001111 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001112
1113 // FIXME: Set CXX-structors flag.
1114 if (IsClassHidden(ID->getClassInterface()))
1115 Flags |= eClassFlags_Hidden;
1116
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001117 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1118 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1119 e = ID->instmeth_end(); i != e; ++i) {
1120 // Instance methods should always be defined.
1121 InstanceMethods.push_back(GetMethodConstant(*i));
1122 }
1123 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1124 e = ID->classmeth_end(); i != e; ++i) {
1125 // Class methods should always be defined.
1126 ClassMethods.push_back(GetMethodConstant(*i));
1127 }
1128
1129 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1130 e = ID->propimpl_end(); i != e; ++i) {
1131 ObjCPropertyImplDecl *PID = *i;
1132
1133 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1134 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1135
1136 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1137 if (llvm::Constant *C = GetMethodConstant(MD))
1138 InstanceMethods.push_back(C);
1139 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1140 if (llvm::Constant *C = GetMethodConstant(MD))
1141 InstanceMethods.push_back(C);
1142 }
1143 }
1144
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001145 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001146 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001147 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001148 // Record a reference to the super class.
1149 LazySymbols.insert(Super->getIdentifier());
1150
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001151 Values[ 1] =
1152 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1153 ObjCTypes.ClassPtrTy);
1154 } else {
1155 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1156 }
1157 Values[ 2] = GetClassName(ID->getIdentifier());
1158 // Version is always 0.
1159 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1160 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1161 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1162 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001163 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001164 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001165 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001166 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001167 // cache is always NULL.
1168 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1169 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001170 // FIXME: Set ivar_layout
1171 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001172 Values[11] = EmitClassExtension(ID);
1173 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1174 Values);
1175
1176 llvm::GlobalVariable *GV =
1177 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1178 llvm::GlobalValue::InternalLinkage,
1179 Init,
1180 std::string("\01L_OBJC_CLASS_")+ClassName,
1181 &CGM.getModule());
1182 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1183 UsedGlobals.push_back(GV);
1184 // FIXME: Why?
1185 GV->setAlignment(32);
1186 DefinedClasses.push_back(GV);
1187}
1188
1189llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1190 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001191 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001192 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001193 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001194 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001195
1196 if (IsClassHidden(ID->getClassInterface()))
1197 Flags |= eClassFlags_Hidden;
1198
1199 std::vector<llvm::Constant*> Values(12);
1200 // The isa for the metaclass is the root of the hierarchy.
1201 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1202 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1203 Root = Super;
1204 Values[ 0] =
1205 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1206 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001207 // The super class for the metaclass is emitted as the name of the
1208 // super class. The runtime fixes this up to point to the
1209 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001210 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1211 Values[ 1] =
1212 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1213 ObjCTypes.ClassPtrTy);
1214 } else {
1215 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1216 }
1217 Values[ 2] = GetClassName(ID->getIdentifier());
1218 // Version is always 0.
1219 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1220 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1221 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1222 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001223 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001224 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001225 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001226 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001227 // cache is always NULL.
1228 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1229 Values[ 9] = Protocols;
1230 // ivar_layout for metaclass is always NULL.
1231 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1232 // The class extension is always unused for metaclasses.
1233 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1234 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1235 Values);
1236
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001237 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001238 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001239
1240 // Check for a forward reference.
1241 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1242 if (GV) {
1243 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1244 "Forward metaclass reference has incorrect type.");
1245 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1246 GV->setInitializer(Init);
1247 } else {
1248 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1249 llvm::GlobalValue::InternalLinkage,
1250 Init, Name,
1251 &CGM.getModule());
1252 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001253 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1254 UsedGlobals.push_back(GV);
1255 // FIXME: Why?
1256 GV->setAlignment(32);
1257
1258 return GV;
1259}
1260
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001261llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001262 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001263
1264 // FIXME: Should we look these up somewhere other than the
1265 // module. Its a bit silly since we only generate these while
1266 // processing an implementation, so exactly one pointer would work
1267 // if know when we entered/exitted an implementation block.
1268
1269 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001270 // Previously, metaclass with internal linkage may have been defined.
1271 // pass 'true' as 2nd argument so it is returned.
1272 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001273 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1274 "Forward metaclass reference has incorrect type.");
1275 return GV;
1276 } else {
1277 // Generate as an external reference to keep a consistent
1278 // module. This will be patched up when we emit the metaclass.
1279 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1280 llvm::GlobalValue::ExternalLinkage,
1281 0,
1282 Name,
1283 &CGM.getModule());
1284 }
1285}
1286
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001287/*
1288 struct objc_class_ext {
1289 uint32_t size;
1290 const char *weak_ivar_layout;
1291 struct _objc_property_list *properties;
1292 };
1293*/
1294llvm::Constant *
1295CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1296 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001297 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001298
1299 std::vector<llvm::Constant*> Values(3);
1300 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001301 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001302 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001303 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001304 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001305
1306 // Return null if no extension bits are used.
1307 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1308 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1309
1310 llvm::Constant *Init =
1311 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1312 llvm::GlobalVariable *GV =
1313 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1314 llvm::GlobalValue::InternalLinkage,
1315 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001316 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001317 &CGM.getModule());
1318 // No special section, but goes in llvm.used
1319 UsedGlobals.push_back(GV);
1320
1321 return GV;
1322}
1323
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001324/// countInheritedIvars - count number of ivars in class and its super class(s)
1325///
1326static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1327 int count = 0;
1328 if (!OI)
1329 return 0;
1330 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1331 if (SuperClass)
1332 count += countInheritedIvars(SuperClass);
1333 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1334 E = OI->ivar_end(); I != E; ++I)
1335 ++count;
1336 return count;
1337}
1338
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001339/*
1340 struct objc_ivar {
1341 char *ivar_name;
1342 char *ivar_type;
1343 int ivar_offset;
1344 };
1345
1346 struct objc_ivar_list {
1347 int ivar_count;
1348 struct objc_ivar list[count];
1349 };
1350 */
1351llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1352 bool ForClass,
1353 const llvm::Type *InterfaceTy) {
1354 std::vector<llvm::Constant*> Ivars, Ivar(3);
1355
1356 // When emitting the root class GCC emits ivar entries for the
1357 // actual class structure. It is not clear if we need to follow this
1358 // behavior; for now lets try and get away with not doing it. If so,
1359 // the cleanest solution would be to make up an ObjCInterfaceDecl
1360 // for the class.
1361 if (ForClass)
1362 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1363
1364 const llvm::StructLayout *Layout =
1365 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001366 ObjCInterfaceDecl *OID =
1367 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1368 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1369 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1370 RecordDecl::field_iterator ifield = RD->field_begin();
1371 while (countSuperClassIvars-- > 0)
1372 ++ifield;
1373 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1374 FieldDecl *Field = *ifield;
1375 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1376 getLLVMFieldNo(Field));
1377 if (Field->getIdentifier())
1378 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1379 else
1380 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001381 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001382 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001383 Ivar[1] = GetMethodVarType(TypeStr);
1384 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001385 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001386 }
1387
1388 // Return null for empty list.
1389 if (Ivars.empty())
1390 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1391
1392 std::vector<llvm::Constant*> Values(2);
1393 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1394 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1395 Ivars.size());
1396 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1397 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1398
1399 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1400 "\01L_OBJC_INSTANCE_VARIABLES_");
1401 llvm::GlobalVariable *GV =
1402 new llvm::GlobalVariable(Init->getType(), false,
1403 llvm::GlobalValue::InternalLinkage,
1404 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001405 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001406 &CGM.getModule());
1407 if (ForClass) {
1408 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1409 // FIXME: Why is this only here?
1410 GV->setAlignment(32);
1411 } else {
1412 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1413 }
1414 UsedGlobals.push_back(GV);
1415 return llvm::ConstantExpr::getBitCast(GV,
1416 ObjCTypes.IvarListPtrTy);
1417}
1418
1419/*
1420 struct objc_method {
1421 SEL method_name;
1422 char *method_types;
1423 void *method;
1424 };
1425
1426 struct objc_method_list {
1427 struct objc_method_list *obsolete;
1428 int count;
1429 struct objc_method methods_list[count];
1430 };
1431*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001432
1433/// GetMethodConstant - Return a struct objc_method constant for the
1434/// given method if it has been defined. The result is null if the
1435/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001436llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001437 // FIXME: Use DenseMap::lookup
1438 llvm::Function *Fn = MethodDefinitions[MD];
1439 if (!Fn)
1440 return 0;
1441
1442 std::vector<llvm::Constant*> Method(3);
1443 Method[0] =
1444 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1445 ObjCTypes.SelectorPtrTy);
1446 Method[1] = GetMethodVarType(MD);
1447 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1448 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1449}
1450
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001451llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1452 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001453 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001454 // Return null for empty list.
1455 if (Methods.empty())
1456 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1457
1458 std::vector<llvm::Constant*> Values(3);
1459 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1460 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1461 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1462 Methods.size());
1463 Values[2] = llvm::ConstantArray::get(AT, Methods);
1464 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1465
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001466 llvm::GlobalVariable *GV =
1467 new llvm::GlobalVariable(Init->getType(), false,
1468 llvm::GlobalValue::InternalLinkage,
1469 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001470 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001471 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001472 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001473 UsedGlobals.push_back(GV);
1474 return llvm::ConstantExpr::getBitCast(GV,
1475 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001476}
1477
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001478llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD,
1479 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001480 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001481 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001482
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001483 const llvm::FunctionType *MethodTy =
1484 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001485 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001486 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001487 llvm::GlobalValue::InternalLinkage,
1488 Name,
1489 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001490 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001491
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001492 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001493}
1494
1495llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001496 // Abuse this interface function as a place to finalize.
1497 FinishModule();
1498
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001499 return NULL;
1500}
1501
Daniel Dunbar49f66022008-09-24 03:38:44 +00001502llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1503 return ObjCTypes.GetPropertyFn;
1504}
1505
1506llvm::Function *CGObjCMac::GetPropertySetFunction() {
1507 return ObjCTypes.SetPropertyFn;
1508}
1509
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001510llvm::Function *CGObjCMac::EnumerationMutationFunction()
1511{
1512 return ObjCTypes.EnumerationMutationFn;
1513}
1514
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001515/*
1516
1517Objective-C setjmp-longjmp (sjlj) Exception Handling
1518--
1519
1520The basic framework for a @try-catch-finally is as follows:
1521{
1522 objc_exception_data d;
1523 id _rethrow = null;
1524
1525 objc_exception_try_enter(&d);
1526 if (!setjmp(d.jmp_buf)) {
1527 ... try body ...
1528 } else {
1529 // exception path
1530 id _caught = objc_exception_extract(&d);
1531
1532 // enter new try scope for handlers
1533 if (!setjmp(d.jmp_buf)) {
1534 ... match exception and execute catch blocks ...
1535
1536 // fell off end, rethrow.
1537 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001538 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001539 } else {
1540 // exception in catch block
1541 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001542 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001543 }
1544 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001545 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001546
1547finally:
1548 // match either the initial try_enter or the catch try_enter,
1549 // depending on the path followed.
1550 objc_exception_try_exit(&d);
1551finally_no_exit:
1552 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001553 ... dispatch to finally destination ...
1554
1555finally_rethrow:
1556 objc_exception_throw(_rethrow);
1557
1558finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001559}
1560
1561This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001562uses _rethrow to determine if objc_exception_try_exit should be called
1563and if the object should be rethrown. This breaks in the face of
1564throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001565
1566We specialize this framework for a few particular circumstances:
1567
1568 - If there are no catch blocks, then we avoid emitting the second
1569 exception handling context.
1570
1571 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1572 e)) we avoid emitting the code to rethrow an uncaught exception.
1573
1574 - FIXME: If there is no @finally block we can do a few more
1575 simplifications.
1576
1577Rethrows and Jumps-Through-Finally
1578--
1579
1580Support for implicit rethrows and jumping through the finally block is
1581handled by storing the current exception-handling context in
1582ObjCEHStack.
1583
Daniel Dunbar898d5082008-09-30 01:06:03 +00001584In order to implement proper @finally semantics, we support one basic
1585mechanism for jumping through the finally block to an arbitrary
1586destination. Constructs which generate exits from a @try or @catch
1587block use this mechanism to implement the proper semantics by chaining
1588jumps, as necessary.
1589
1590This mechanism works like the one used for indirect goto: we
1591arbitrarily assign an ID to each destination and store the ID for the
1592destination in a variable prior to entering the finally block. At the
1593end of the finally block we simply create a switch to the proper
1594destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001595
1596Code gen for @synchronized(expr) stmt;
1597Effectively generating code for:
1598objc_sync_enter(expr);
1599@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001600*/
1601
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001602void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1603 const Stmt &S) {
1604 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001605 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001606 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1607 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1608 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1609 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001610 llvm::Value *DestCode =
1611 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1612
1613 // Generate jump code. Done here so we can directly add things to
1614 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001615 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001616 llvm::SwitchInst *FinallySwitch =
1617 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1618 FinallyEnd, 10, FinallyJump);
1619
1620 // Push an EH context entry, used for handling rethrows and jumps
1621 // through finally.
1622 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1623 FinallySwitch, DestCode);
1624 CGF.ObjCEHStack.push_back(&EHEntry);
1625
1626 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001627 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1628 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001629 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1630 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001631 if (!isTry) {
1632 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001633 llvm::Value *Arg = CGF.EmitScalarExpr(
1634 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1635 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1636 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001637 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001638
1639 // Enter a new try block and call setjmp.
1640 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1641 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1642 "jmpbufarray");
1643 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1644 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1645 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001646
Daniel Dunbar55e87422008-11-11 02:29:29 +00001647 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1648 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001649 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001650 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001651
1652 // Emit the @try block.
1653 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001654 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1655 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001656 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001657
1658 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001659 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001660
1661 // Retrieve the exception object. We may emit multiple blocks but
1662 // nothing can cross this so the value is already in SSA form.
1663 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1664 ExceptionData,
1665 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001666 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001667 if (!isTry)
1668 {
1669 CGF.Builder.CreateStore(Caught, RethrowPtr);
1670 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1671 }
1672 else if (const ObjCAtCatchStmt* CatchStmt =
1673 cast<ObjCAtTryStmt>(S).getCatchStmts())
1674 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001675 // Enter a new exception try block (in case a @catch block throws
1676 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001677 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001678
Anders Carlsson80f25672008-09-09 17:59:25 +00001679 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1680 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001681 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001682
Daniel Dunbar55e87422008-11-11 02:29:29 +00001683 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1684 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001685 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001686
1687 CGF.EmitBlock(CatchBlock);
1688
Daniel Dunbar55e40722008-09-27 07:03:52 +00001689 // Handle catch list. As a special case we check if everything is
1690 // matched and avoid generating code for falling off the end if
1691 // so.
1692 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001693 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001694 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001695
Anders Carlssondde0a942008-09-11 09:15:33 +00001696 const DeclStmt *CatchParam =
1697 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001698 const VarDecl *VD = 0;
1699 const PointerType *PT = 0;
1700
Anders Carlsson80f25672008-09-09 17:59:25 +00001701 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001702 if (!CatchParam) {
1703 AllMatched = true;
1704 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001705 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001706 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001707
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001708 // catch(id e) always matches.
1709 // FIXME: For the time being we also match id<X>; this should
1710 // be rejected by Sema instead.
1711 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1712 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001713 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001714 }
1715
Daniel Dunbar55e40722008-09-27 07:03:52 +00001716 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001717 if (CatchParam) {
1718 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001719 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001720 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001721 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001722
Anders Carlssondde0a942008-09-11 09:15:33 +00001723 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001724 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001725 break;
1726 }
1727
Daniel Dunbar129271a2008-09-27 07:36:24 +00001728 assert(PT && "Unexpected non-pointer type in @catch");
1729 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001730 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001731 assert(ObjCType && "Catch parameter must have Objective-C type!");
1732
1733 // Check if the @catch block matches the exception object.
1734 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1735
Anders Carlsson80f25672008-09-09 17:59:25 +00001736 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1737 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001738
Daniel Dunbar55e87422008-11-11 02:29:29 +00001739 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001740
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001741 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001742 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001743
1744 // Emit the @catch block.
1745 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001746 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001747 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001748
1749 llvm::Value *Tmp =
1750 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1751 "tmp");
1752 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001753
1754 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001755 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001756
1757 CGF.EmitBlock(NextCatchBlock);
1758 }
1759
Daniel Dunbar55e40722008-09-27 07:03:52 +00001760 if (!AllMatched) {
1761 // None of the handlers caught the exception, so store it to be
1762 // rethrown at the end of the @finally block.
1763 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001764 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001765 }
1766
1767 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001768 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001769 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1770 ExceptionData),
1771 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001772 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001773 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001774 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001775 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001776 }
1777
Daniel Dunbar898d5082008-09-30 01:06:03 +00001778 // Pop the exception-handling stack entry. It is important to do
1779 // this now, because the code in the @finally block is not in this
1780 // context.
1781 CGF.ObjCEHStack.pop_back();
1782
Anders Carlsson80f25672008-09-09 17:59:25 +00001783 // Emit the @finally block.
1784 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001785 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001786
1787 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001788 if (isTry) {
1789 if (const ObjCAtFinallyStmt* FinallyStmt =
1790 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1791 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1792 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001793 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001794 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001795 // For @synchronized, call objc_sync_enter(sync.expr)
1796 llvm::Value *Arg = CGF.EmitScalarExpr(
1797 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1798 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1799 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1800 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001801
Daniel Dunbar898d5082008-09-30 01:06:03 +00001802 CGF.EmitBlock(FinallyJump);
1803
1804 CGF.EmitBlock(FinallyRethrow);
1805 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1806 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001807 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001808
1809 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001810}
1811
1812void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001813 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001814 llvm::Value *ExceptionAsObject;
1815
1816 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1817 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1818 ExceptionAsObject =
1819 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1820 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001821 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001822 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001823 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001824 }
1825
1826 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001827 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001828
1829 // Clear the insertion point to indicate we are in unreachable code.
1830 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001831}
1832
Daniel Dunbar898d5082008-09-30 01:06:03 +00001833void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1834 llvm::BasicBlock *Dst,
1835 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001836 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001837 return;
1838
1839 // Find the destination code for this block. We always use 0 for the
1840 // fallthrough block (default destination).
1841 llvm::SwitchInst *SI = E->FinallySwitch;
1842 llvm::ConstantInt *ID;
1843 if (Dst == SI->getDefaultDest()) {
1844 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1845 } else {
1846 ID = SI->findCaseDest(Dst);
1847 if (!ID) {
1848 // No code found, get a new unique one by just using the number
1849 // of switch successors.
1850 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1851 SI->addCase(ID, Dst);
1852 }
1853 }
1854
1855 // Set the destination code and branch.
1856 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001857 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001858}
1859
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001860/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001861/// object: objc_read_weak (id *src)
1862///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001863llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001864 llvm::Value *AddrWeakObj)
1865{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001866 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001867 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001868 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001869 return read_weak;
1870}
1871
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001872/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
1873/// objc_assign_weak (id src, id *dst)
1874///
1875void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1876 llvm::Value *src, llvm::Value *dst)
1877{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001878 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1879 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001880 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
1881 src, dst, "weakassign");
1882 return;
1883}
1884
Fariborz Jahanian58626502008-11-19 00:59:10 +00001885/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
1886/// objc_assign_global (id src, id *dst)
1887///
1888void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1889 llvm::Value *src, llvm::Value *dst)
1890{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001891 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1892 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001893 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
1894 src, dst, "globalassign");
1895 return;
1896}
1897
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001898/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
1899/// objc_assign_ivar (id src, id *dst)
1900///
1901void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1902 llvm::Value *src, llvm::Value *dst)
1903{
1904 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1905 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
1906 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
1907 src, dst, "assignivar");
1908 return;
1909}
1910
Fariborz Jahanian58626502008-11-19 00:59:10 +00001911/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
1912/// objc_assign_strongCast (id src, id *dst)
1913///
1914void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1915 llvm::Value *src, llvm::Value *dst)
1916{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001917 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1918 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001919 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
1920 src, dst, "weakassign");
1921 return;
1922}
1923
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001924/* *** Private Interface *** */
1925
1926/// EmitImageInfo - Emit the image info marker used to encode some module
1927/// level information.
1928///
1929/// See: <rdr://4810609&4810587&4810587>
1930/// struct IMAGE_INFO {
1931/// unsigned version;
1932/// unsigned flags;
1933/// };
1934enum ImageInfoFlags {
1935 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1936 eImageInfo_GarbageCollected = (1 << 1),
1937 eImageInfo_GCOnly = (1 << 2)
1938};
1939
1940void CGObjCMac::EmitImageInfo() {
1941 unsigned version = 0; // Version is unused?
1942 unsigned flags = 0;
1943
1944 // FIXME: Fix and continue?
1945 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1946 flags |= eImageInfo_GarbageCollected;
1947 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1948 flags |= eImageInfo_GCOnly;
1949
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001950 // Emitted as int[2];
1951 llvm::Constant *values[2] = {
1952 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1953 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1954 };
1955 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001956 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001957 new llvm::GlobalVariable(AT, true,
1958 llvm::GlobalValue::InternalLinkage,
1959 llvm::ConstantArray::get(AT, values, 2),
1960 "\01L_OBJC_IMAGE_INFO",
1961 &CGM.getModule());
1962
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001963 if (ObjCABI == 1) {
1964 GV->setSection("__OBJC, __image_info,regular");
1965 } else {
1966 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1967 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001968
1969 UsedGlobals.push_back(GV);
1970}
1971
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001972
1973// struct objc_module {
1974// unsigned long version;
1975// unsigned long size;
1976// const char *name;
1977// Symtab symtab;
1978// };
1979
1980// FIXME: Get from somewhere
1981static const int ModuleVersion = 7;
1982
1983void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001984 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001985
1986 std::vector<llvm::Constant*> Values(4);
1987 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1988 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001989 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001990 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001991 Values[3] = EmitModuleSymbols();
1992
1993 llvm::GlobalVariable *GV =
1994 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1995 llvm::GlobalValue::InternalLinkage,
1996 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1997 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001998 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001999 &CGM.getModule());
2000 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2001 UsedGlobals.push_back(GV);
2002}
2003
2004llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002005 unsigned NumClasses = DefinedClasses.size();
2006 unsigned NumCategories = DefinedCategories.size();
2007
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002008 // Return null if no symbols were defined.
2009 if (!NumClasses && !NumCategories)
2010 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2011
2012 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002013 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2014 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2015 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2016 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2017
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002018 // The runtime expects exactly the list of defined classes followed
2019 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002020 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002021 for (unsigned i=0; i<NumClasses; i++)
2022 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2023 ObjCTypes.Int8PtrTy);
2024 for (unsigned i=0; i<NumCategories; i++)
2025 Symbols[NumClasses + i] =
2026 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2027 ObjCTypes.Int8PtrTy);
2028
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002029 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002030 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002031 NumClasses + NumCategories),
2032 Symbols);
2033
2034 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2035
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002036 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002037 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002038 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002039 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002040 "\01L_OBJC_SYMBOLS",
2041 &CGM.getModule());
2042 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2043 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002044 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2045}
2046
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002047llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002048 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002049 LazySymbols.insert(ID->getIdentifier());
2050
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002051 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2052
2053 if (!Entry) {
2054 llvm::Constant *Casted =
2055 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2056 ObjCTypes.ClassPtrTy);
2057 Entry =
2058 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2059 llvm::GlobalValue::InternalLinkage,
2060 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2061 &CGM.getModule());
2062 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2063 UsedGlobals.push_back(Entry);
2064 }
2065
2066 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002067}
2068
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002069llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002070 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2071
2072 if (!Entry) {
2073 llvm::Constant *Casted =
2074 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2075 ObjCTypes.SelectorPtrTy);
2076 Entry =
2077 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2078 llvm::GlobalValue::InternalLinkage,
2079 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2080 &CGM.getModule());
2081 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2082 UsedGlobals.push_back(Entry);
2083 }
2084
2085 return Builder.CreateLoad(Entry, false, "tmp");
2086}
2087
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002088llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
2089 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002090
2091 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002092 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002093 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002094 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002095 llvm::GlobalValue::InternalLinkage,
2096 C, "\01L_OBJC_CLASS_NAME_",
2097 &CGM.getModule());
2098 Entry->setSection("__TEXT,__cstring,cstring_literals");
2099 UsedGlobals.push_back(Entry);
2100 }
2101
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002102 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002103}
2104
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002105llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002106 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2107
2108 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002109 // FIXME: Avoid std::string copying.
2110 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002111 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002112 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002113 llvm::GlobalValue::InternalLinkage,
2114 C, "\01L_OBJC_METH_VAR_NAME_",
2115 &CGM.getModule());
2116 Entry->setSection("__TEXT,__cstring,cstring_literals");
2117 UsedGlobals.push_back(Entry);
2118 }
2119
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002120 return getConstantGEP(Entry, 0, 0);
2121}
2122
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002123// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002124llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002125 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2126}
2127
2128// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002129llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002130 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2131}
2132
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002133llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002134 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002135
2136 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002137 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002138 Entry =
2139 new llvm::GlobalVariable(C->getType(), false,
2140 llvm::GlobalValue::InternalLinkage,
2141 C, "\01L_OBJC_METH_VAR_TYPE_",
2142 &CGM.getModule());
2143 Entry->setSection("__TEXT,__cstring,cstring_literals");
2144 UsedGlobals.push_back(Entry);
2145 }
2146
2147 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002148}
2149
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002151llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002152 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002153 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2154 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155 return GetMethodVarType(TypeStr);
2156}
2157
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002158// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002159llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002160 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2161
2162 if (!Entry) {
2163 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2164 Entry =
2165 new llvm::GlobalVariable(C->getType(), false,
2166 llvm::GlobalValue::InternalLinkage,
2167 C, "\01L_OBJC_PROP_NAME_ATTR_",
2168 &CGM.getModule());
2169 Entry->setSection("__TEXT,__cstring,cstring_literals");
2170 UsedGlobals.push_back(Entry);
2171 }
2172
2173 return getConstantGEP(Entry, 0, 0);
2174}
2175
2176// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002177// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002178llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002179 const Decl *Container) {
2180 std::string TypeStr;
2181 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002182 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2183}
2184
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002185void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2186 const ObjCContainerDecl *CD,
2187 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002188 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002189 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002190 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002191 assert (CD && "Missing container decl in GetNameForMethod");
2192 NameOut += CD->getNameAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +00002193 NameOut += ' ';
2194 NameOut += D->getSelector().getAsString();
2195 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002196}
2197
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002198void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002199 EmitModuleInfo();
2200
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002201 // Emit the dummy bodies for any protocols which were referenced but
2202 // never defined.
2203 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2204 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2205 if (i->second->hasInitializer())
2206 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002207
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002208 std::vector<llvm::Constant*> Values(5);
2209 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2210 Values[1] = GetClassName(i->first);
2211 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2212 Values[3] = Values[4] =
2213 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2214 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2215 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2216 Values));
2217 }
2218
2219 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002220 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002221 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002223 }
2224
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002226 llvm::GlobalValue *GV =
2227 new llvm::GlobalVariable(AT, false,
2228 llvm::GlobalValue::AppendingLinkage,
2229 llvm::ConstantArray::get(AT, Used),
2230 "llvm.used",
2231 &CGM.getModule());
2232
2233 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002234
2235 // Add assembler directives to add lazy undefined symbol references
2236 // for classes which are referenced but not defined. This is
2237 // important for correct linker interaction.
2238
2239 // FIXME: Uh, this isn't particularly portable.
2240 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002241
2242 if (!CGM.getModule().getModuleInlineAsm().empty())
2243 s << "\n";
2244
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002245 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2246 e = LazySymbols.end(); i != e; ++i) {
2247 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2248 }
2249 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2250 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002251 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002252 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2253 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002254
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002255 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002256}
2257
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002258CGObjCModernMac::CGObjCModernMac(CodeGen::CodeGenModule &cgm)
2259 : CGObjCCommonMac(cgm),
2260 ObjCTypes(cgm)
2261{
2262 ObjCABI = 2;
2263}
2264
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002265/* *** */
2266
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002267ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2268: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002269{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2271 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002272
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002274 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002275 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002278 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002279 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002280 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002281
2282 // FIXME: It would be nice to unify this with the opaque type, so
2283 // that the IR comes out a bit cleaner.
2284 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2285 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002286
2287 // I'm not sure I like this. The implicit coordination is a bit
2288 // gross. We should solve this in a reasonable fashion because this
2289 // is a pretty common task (match some runtime data structure with
2290 // an LLVM data structure).
2291
2292 // FIXME: This is leaked.
2293 // FIXME: Merge with rewriter code?
2294
2295 // struct _objc_super {
2296 // id self;
2297 // Class cls;
2298 // }
2299 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2300 SourceLocation(),
2301 &Ctx.Idents.get("_objc_super"));
2302 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2303 Ctx.getObjCIdType(), 0, false));
2304 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2305 Ctx.getObjCClassType(), 0, false));
2306 RD->completeDefinition(Ctx);
2307
2308 SuperCTy = Ctx.getTagDeclType(RD);
2309 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2310
2311 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
2312 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2313}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002314
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002315ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2316 : ObjCCommonTypesHelper(cgm)
2317{
2318 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2319 ASTContext &Ctx = CGM.getContext();
2320
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002321 // struct _objc_method_description {
2322 // SEL name;
2323 // char *types;
2324 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002325 MethodDescriptionTy =
2326 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002328 NULL);
2329 CGM.getModule().addTypeName("struct._objc_method_description",
2330 MethodDescriptionTy);
2331
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002332 // struct _objc_method_description_list {
2333 // int count;
2334 // struct _objc_method_description[1];
2335 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002336 MethodDescriptionListTy =
2337 llvm::StructType::get(IntTy,
2338 llvm::ArrayType::get(MethodDescriptionTy, 0),
2339 NULL);
2340 CGM.getModule().addTypeName("struct._objc_method_description_list",
2341 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002342
2343 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002344 MethodDescriptionListPtrTy =
2345 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2346
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002347 // struct _objc_property {
2348 // char *name;
2349 // char *attributes;
2350 // }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002351 PropertyTy = llvm::StructType::get(Int8PtrTy,
2352 Int8PtrTy,
2353 NULL);
2354 CGM.getModule().addTypeName("struct._objc_property",
2355 PropertyTy);
2356
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002357 // struct _objc_property_list {
2358 // uint32_t entsize; // sizeof(struct _objc_property)
2359 // uint32_t count_of_properties;
2360 // struct _objc_property prop_list[count_of_properties];
2361 // }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002362 PropertyListTy = llvm::StructType::get(IntTy,
2363 IntTy,
2364 llvm::ArrayType::get(PropertyTy, 0),
2365 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002366 CGM.getModule().addTypeName("struct._objc_property_list",
2367 PropertyListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002368 // struct _objc_property_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002369 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2370
2371 // Protocol description structures
2372
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002373 // struct _objc_protocol_extension {
2374 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2375 // struct _objc_method_description_list *optional_instance_methods;
2376 // struct _objc_method_description_list *optional_class_methods;
2377 // struct _objc_property_list *instance_properties;
2378 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002379 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002380 llvm::StructType::get(IntTy,
2381 MethodDescriptionListPtrTy,
2382 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002383 PropertyListPtrTy,
2384 NULL);
2385 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2386 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002387
2388 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002389 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2390
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002391 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002392
2393 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2394 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2395
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002396 const llvm::Type *T =
2397 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2398 LongTy,
2399 llvm::ArrayType::get(ProtocolTyHolder, 0),
2400 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002401 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2402
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002403 // struct _objc_protocol {
2404 // struct _objc_protocol_extension *isa;
2405 // char *protocol_name;
2406 // struct _objc_protocol **_objc_protocol_list;
2407 // struct _objc_method_description_list *instance_methods;
2408 // struct _objc_method_description_list *class_methods;
2409 // }
2410 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002412 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2413 MethodDescriptionListPtrTy,
2414 MethodDescriptionListPtrTy,
2415 NULL);
2416 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2417
2418 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2419 CGM.getModule().addTypeName("struct._objc_protocol_list",
2420 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002421 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002422 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2423
2424 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002425 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002426 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427
2428 // Class description structures
2429
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002430 // struct _objc_ivar {
2431 // char *ivar_name;
2432 // char *ivar_type;
2433 // int ivar_offset;
2434 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002435 IvarTy = llvm::StructType::get(Int8PtrTy,
2436 Int8PtrTy,
2437 IntTy,
2438 NULL);
2439 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2440
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002441 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442 IvarListTy = llvm::OpaqueType::get();
2443 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2444 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2445
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002446 // struct _objc_method {
2447 // SEL _cmd;
2448 // char *method_type;
2449 // char *_imp;
2450 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 MethodTy = llvm::StructType::get(SelectorPtrTy,
2452 Int8PtrTy,
2453 Int8PtrTy,
2454 NULL);
2455 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
2456
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002457 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 MethodListTy = llvm::OpaqueType::get();
2459 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2460 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2461
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002462 // struct _objc_cache *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002463 CacheTy = llvm::OpaqueType::get();
2464 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2465 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
2466
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002467 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002468 ClassExtensionTy =
2469 llvm::StructType::get(IntTy,
2470 Int8PtrTy,
2471 PropertyListPtrTy,
2472 NULL);
2473 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2474 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2475
2476 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2477
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002478 // struct _objc_class {
2479 // Class isa;
2480 // Class super_class;
2481 // char *name;
2482 // long version;
2483 // long info;
2484 // long instance_size;
2485 // struct _objc_ivar_list *ivars;
2486 // struct _objc_method_list *methods;
2487 // struct _objc_cache *cache;
2488 // struct _objc_protocol_list *protocols;
2489 // char *ivar_layout;
2490 // struct _objc_class_ext *ext;
2491 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2493 llvm::PointerType::getUnqual(ClassTyHolder),
2494 Int8PtrTy,
2495 LongTy,
2496 LongTy,
2497 LongTy,
2498 IvarListPtrTy,
2499 MethodListPtrTy,
2500 CachePtrTy,
2501 ProtocolListPtrTy,
2502 Int8PtrTy,
2503 ClassExtensionPtrTy,
2504 NULL);
2505 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2506
2507 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2508 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2509 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2510
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002511 // struct _objc_category {
2512 // char *category_name;
2513 // char *class_name;
2514 // struct _objc_method_list *instance_method;
2515 // struct _objc_method_list *class_method;
2516 // uint32_t size; // sizeof(struct _objc_category)
2517 // struct _objc_property_list *instance_properties;// category's @property
2518 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002519 CategoryTy = llvm::StructType::get(Int8PtrTy,
2520 Int8PtrTy,
2521 MethodListPtrTy,
2522 MethodListPtrTy,
2523 ProtocolListPtrTy,
2524 IntTy,
2525 PropertyListPtrTy,
2526 NULL);
2527 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2528
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002529 // Global metadata structures
2530
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002531 // struct _objc_symtab {
2532 // long sel_ref_cnt;
2533 // SEL *refs;
2534 // short cls_def_cnt;
2535 // short cat_def_cnt;
2536 // char *defs[cls_def_cnt + cat_def_cnt];
2537 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002538 SymtabTy = llvm::StructType::get(LongTy,
2539 SelectorPtrTy,
2540 ShortTy,
2541 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002542 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002543 NULL);
2544 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2545 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2546
2547 ModuleTy =
2548 llvm::StructType::get(LongTy,
2549 LongTy,
2550 Int8PtrTy,
2551 SymtabPtrTy,
2552 NULL);
2553 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002554
Daniel Dunbar49f66022008-09-24 03:38:44 +00002555 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002556
2557 std::vector<const llvm::Type*> Params;
2558 Params.push_back(ObjectPtrTy);
2559 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002560 MessageSendFn =
2561 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2562 Params,
2563 true),
2564 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002565
2566 Params.clear();
2567 Params.push_back(Int8PtrTy);
2568 Params.push_back(ObjectPtrTy);
2569 Params.push_back(SelectorPtrTy);
2570 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002571 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2572 Params,
2573 true),
2574 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002575
2576 Params.clear();
2577 Params.push_back(ObjectPtrTy);
2578 Params.push_back(SelectorPtrTy);
2579 // FIXME: This should be long double on x86_64?
2580 MessageSendFpretFn =
2581 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2582 Params,
2583 true),
2584 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002585
2586 Params.clear();
2587 Params.push_back(SuperPtrTy);
2588 Params.push_back(SelectorPtrTy);
2589 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002590 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2591 Params,
2592 true),
2593 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002594
2595 Params.clear();
2596 Params.push_back(Int8PtrTy);
2597 Params.push_back(SuperPtrTy);
2598 Params.push_back(SelectorPtrTy);
2599 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002600 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2601 Params,
2602 true),
2603 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002604
2605 // There is no objc_msgSendSuper_fpret? How can that work?
2606 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002607
Daniel Dunbar49f66022008-09-24 03:38:44 +00002608 // Property manipulation functions.
2609
2610 Params.clear();
2611 Params.push_back(ObjectPtrTy);
2612 Params.push_back(SelectorPtrTy);
2613 Params.push_back(LongTy);
2614 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2615 GetPropertyFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2617 Params,
2618 false),
2619 "objc_getProperty");
2620
Daniel Dunbar49f66022008-09-24 03:38:44 +00002621 Params.clear();
2622 Params.push_back(ObjectPtrTy);
2623 Params.push_back(SelectorPtrTy);
2624 Params.push_back(LongTy);
Daniel Dunbar86957eb2008-09-24 06:32:09 +00002625 Params.push_back(ObjectPtrTy);
Daniel Dunbar49f66022008-09-24 03:38:44 +00002626 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2627 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2628 SetPropertyFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002629 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2630 Params,
2631 false),
2632 "objc_setProperty");
Daniel Dunbar49f66022008-09-24 03:38:44 +00002633
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002634 // Enumeration mutation.
Daniel Dunbar49f66022008-09-24 03:38:44 +00002635
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002636 Params.clear();
2637 Params.push_back(ObjectPtrTy);
2638 EnumerationMutationFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002639 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2640 Params,
2641 false),
2642 "objc_enumerationMutation");
Anders Carlsson124526b2008-09-09 10:10:21 +00002643
2644 // FIXME: This is the size of the setjmp buffer and should be
2645 // target specific. 18 is what's used on 32-bit X86.
2646 uint64_t SetJmpBufferSize = 18;
2647
2648 // Exceptions
2649 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002650 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002651
2652 ExceptionDataTy =
2653 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2654 SetJmpBufferSize),
2655 StackPtrTy, NULL);
2656 CGM.getModule().addTypeName("struct._objc_exception_data",
2657 ExceptionDataTy);
2658
2659 Params.clear();
2660 Params.push_back(ObjectPtrTy);
2661 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002662 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2663 Params,
2664 false),
2665 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002666
2667 Params.clear();
2668 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2669 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002670 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2671 Params,
2672 false),
2673 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002674 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002675 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2676 Params,
2677 false),
2678 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002679 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002680 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2681 Params,
2682 false),
2683 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002684
2685 Params.clear();
2686 Params.push_back(ClassPtrTy);
2687 Params.push_back(ObjectPtrTy);
2688 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002689 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2690 Params,
2691 false),
2692 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002693
2694 // synchronized APIs
2695 // void objc_sync_enter (id)
2696 Params.clear();
2697 Params.push_back(ObjectPtrTy);
2698 SyncEnterFn =
2699 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2700 Params,
2701 false),
2702 "objc_sync_enter");
2703 // void objc_sync_exit (id)
2704 SyncExitFn =
2705 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2706 Params,
2707 false),
2708 "objc_sync_exit");
2709
Anders Carlsson124526b2008-09-09 10:10:21 +00002710
2711 Params.clear();
2712 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2713 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002714 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2715 Params,
2716 false),
2717 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002718
2719 // gc's API
2720 // id objc_read_weak (id *)
2721 Params.clear();
2722 Params.push_back(PtrObjectPtrTy);
2723 GcReadWeakFn =
2724 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2725 Params,
2726 false),
2727 "objc_read_weak");
2728 // id objc_assign_weak (id, id *)
2729 Params.clear();
2730 Params.push_back(ObjectPtrTy);
2731 Params.push_back(PtrObjectPtrTy);
2732 GcAssignWeakFn =
2733 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2734 Params,
2735 false),
2736 "objc_assign_weak");
2737 GcAssignGlobalFn =
2738 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2739 Params,
2740 false),
2741 "objc_assign_global");
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002742 GcAssignIvarFn =
2743 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2744 Params,
2745 false),
2746 "objc_assign_ivar");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002747 GcAssignStrongCastFn =
2748 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2749 Params,
2750 false),
2751 "objc_assign_strongCast");
2752
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002753}
2754
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002755ObjCModernTypesHelper::ObjCModernTypesHelper(CodeGen::CodeGenModule &cgm)
2756: ObjCCommonTypesHelper(cgm)
2757{
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002758}
2759
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002760/* *** */
2761
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002762CodeGen::CGObjCRuntime *
2763CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002764 return new CGObjCMac(CGM);
2765}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002766
2767CodeGen::CGObjCRuntime *
2768CodeGen::CreateMacModernObjCRuntime(CodeGen::CodeGenModule &CGM) {
2769 return 0;
2770 // return new CGObjCModernMac(CGM);
2771}