blob: 1b0027a96be002712da1998e5ae75c850e28e83d [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000347public:
348 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
349 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000350
351 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000352};
353
354class CGObjCMac : public CGObjCCommonMac {
355private:
356 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000357 /// EmitImageInfo - Emit the image info marker used to encode some module
358 /// level information.
359 void EmitImageInfo();
360
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000361 /// EmitModuleInfo - Another marker encoding module level
362 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000363 void EmitModuleInfo();
364
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000365 /// EmitModuleSymols - Emit module symbols, the list of defined
366 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000367 llvm::Constant *EmitModuleSymbols();
368
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000369 /// FinishModule - Write out global data structures at the end of
370 /// processing a translation unit.
371 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000372
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000373 /// EmitClassExtension - Generate the class extension structure used
374 /// to store the weak ivar layout and properties. The return value
375 /// has type ClassExtensionPtrTy.
376 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
377
378 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
379 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000380 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000381 const ObjCInterfaceDecl *ID);
382
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000383 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000384 QualType ResultType,
385 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000386 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000387 QualType Arg0Ty,
388 bool IsSuper,
389 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000390
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000391 /// EmitIvarList - Emit the ivar list for the given
392 /// implementation. If ForClass is true the list of class ivars
393 /// (i.e. metaclass ivars) is emitted, otherwise the list of
394 /// interface ivars will be emitted. The return value has type
395 /// IvarListPtrTy.
396 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
397 bool ForClass,
398 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000399
400 /// EmitMetaClass - Emit a forward reference to the class structure
401 /// for the metaclass of the given interface. The return value has
402 /// type ClassPtrTy.
403 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
404
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000405 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000406 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000407 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
408 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000409 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000410 const ConstantVector &Methods);
411
412 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
413
414 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000415
416 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000417 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000418 llvm::Constant *EmitMethodList(const std::string &Name,
419 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000420 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000421
422 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000423 /// method declarations.
424 /// - TypeName: The name for the type containing the methods.
425 /// - IsProtocol: True iff these methods are for a protocol.
426 /// - ClassMethds: True iff these are class methods.
427 /// - Required: When true, only "required" methods are
428 /// listed. Similarly, when false only "optional" methods are
429 /// listed. For classes this should always be true.
430 /// - begin, end: The method list to output.
431 ///
432 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000433 llvm::Constant *EmitMethodDescList(const std::string &Name,
434 const char *Section,
435 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000436
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000437 /// EmitPropertyList - Emit the given property list. The return
438 /// value has type PropertyListPtrTy.
439 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000440 const Decl *Container,
441 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000442
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000443 /// GetOrEmitProtocol - Get the protocol object for the given
444 /// declaration, emitting it if necessary. The return value has type
445 /// ProtocolPtrTy.
446 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
447
448 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
449 /// object for the given declaration, emitting it if needed. These
450 /// forward references will be filled in with empty bodies if no
451 /// definition is seen. The return value has type ProtocolPtrTy.
452 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
453
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000454 /// EmitProtocolExtension - Generate the protocol extension
455 /// structure used to store optional instance and class methods, and
456 /// protocol properties. The return value has type
457 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000458 llvm::Constant *
459 EmitProtocolExtension(const ObjCProtocolDecl *PD,
460 const ConstantVector &OptInstanceMethods,
461 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000462
463 /// EmitProtocolList - Generate the list of referenced
464 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +0000465 llvm::Constant *EmitProtocolList(const std::string &Name,
466 ObjCProtocolDecl::protocol_iterator begin,
467 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000468
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000469 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
470 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000471 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000472
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000473 /// GetProtocolRef - Return a reference to the internal protocol
474 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000475 /// defined. The return value has type ProtocolPtrTy.
476 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000477
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000478 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000479 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000480 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000481
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000482public:
483 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000484
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000485 virtual llvm::Function *ModuleInitFunction();
486
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000487 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000488 QualType ResultType,
489 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000490 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000491 bool IsClassMessage,
492 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000493
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000494 virtual CodeGen::RValue
495 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000496 QualType ResultType,
497 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000498 const ObjCInterfaceDecl *Class,
499 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000500 bool IsClassMessage,
501 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000502
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000503 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000504 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000505
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000506 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000507
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000508 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
509 const ObjCContainerDecl *CD=0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000510
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000511 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000513 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000514
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000515 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000516 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000517
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000518 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000519
Daniel Dunbar49f66022008-09-24 03:38:44 +0000520 virtual llvm::Function *GetPropertyGetFunction();
521 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000522 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000523
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000524 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
525 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000526 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
527 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000528 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000529 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000530 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
531 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000532 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
533 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000534 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
535 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000536 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
537 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000538};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000539
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000540class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000541private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000542 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000543 llvm::GlobalVariable* ObjCEmptyCacheVar;
544 llvm::GlobalVariable* ObjCEmptyVtableVar;
545
546 /// FinishNonFragileABIModule - Write out global data structures at the end of
547 /// processing a translation unit.
548 void FinishNonFragileABIModule();
549
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000550public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000551 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000552 // FIXME. All stubs for now!
553 virtual llvm::Function *ModuleInitFunction();
554
555 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
556 QualType ResultType,
557 Selector Sel,
558 llvm::Value *Receiver,
559 bool IsClassMessage,
560 const CallArgList &CallArgs)
561 {return RValue::get(0);}
562
563 virtual CodeGen::RValue
564 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
565 QualType ResultType,
566 Selector Sel,
567 const ObjCInterfaceDecl *Class,
568 llvm::Value *Receiver,
569 bool IsClassMessage,
570 const CallArgList &CallArgs){ return RValue::get(0);}
571
572 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
573 const ObjCInterfaceDecl *ID){ return 0; }
574
575 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
576 { return 0; }
577
578 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
579 const ObjCContainerDecl *CD=0)
580 { return 0; }
581
582 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD)
583 { return; }
584
585 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
586
587 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
588 const ObjCProtocolDecl *PD)
589 { return 0; }
590
591 virtual void GenerateProtocol(const ObjCProtocolDecl *PD){ return; }
592
593 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
594 virtual llvm::Function *GetPropertySetFunction()
595 { return 0; }
596 virtual llvm::Function *EnumerationMutationFunction()
597 { return 0; }
598
599 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
600 const Stmt &S)
601 { return; }
602 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
603 const ObjCAtThrowStmt &S)
604 { return; }
605 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
606 llvm::Value *AddrWeakObj)
607 { return 0; }
608 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
609 llvm::Value *src, llvm::Value *dst)
610 { return; }
611 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
612 llvm::Value *src, llvm::Value *dest)
613 { return; }
614 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
615 llvm::Value *src, llvm::Value *dest)
616 { return; }
617 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
618 llvm::Value *src, llvm::Value *dest)
619 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000620};
621
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000622} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000623
624/* *** Helper Functions *** */
625
626/// getConstantGEP() - Help routine to construct simple GEPs.
627static llvm::Constant *getConstantGEP(llvm::Constant *C,
628 unsigned idx0,
629 unsigned idx1) {
630 llvm::Value *Idxs[] = {
631 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
632 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
633 };
634 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
635}
636
637/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000638
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000639CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
640 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000641{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000642 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000643 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000644}
645
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000646/// GetClass - Return a reference to the class for the given interface
647/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000648llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000649 const ObjCInterfaceDecl *ID) {
650 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000651}
652
653/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000654llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000655 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000656}
657
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000658/// Generate a constant CFString object.
659/*
660 struct __builtin_CFString {
661 const int *isa; // point to __CFConstantStringClassReference
662 int flags;
663 const char *str;
664 long length;
665 };
666*/
667
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000668llvm::Constant *CGObjCCommonMac::GenerateConstantString(
669 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000670 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000671}
672
673/// Generates a message send where the super is the receiver. This is
674/// a message send to self with special delivery semantics indicating
675/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000676CodeGen::RValue
677CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000678 QualType ResultType,
679 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000680 const ObjCInterfaceDecl *Class,
681 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000682 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000683 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000684 // Create and init a super structure; this is a (receiver, class)
685 // pair we will pass to objc_msgSendSuper.
686 llvm::Value *ObjCSuper =
687 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
688 llvm::Value *ReceiverAsObject =
689 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
690 CGF.Builder.CreateStore(ReceiverAsObject,
691 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000692
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000693 // If this is a class message the metaclass is passed as the target.
694 llvm::Value *Target;
695 if (IsClassMessage) {
696 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
697 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
698 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
699 Target = Super;
700 } else {
701 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
702 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000703 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
704 // and ObjCTypes types.
705 const llvm::Type *ClassTy =
706 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000707 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000708 CGF.Builder.CreateStore(Target,
709 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
710
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000711 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000712 ObjCSuper, ObjCTypes.SuperPtrCTy,
713 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000714}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000715
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000716/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000717CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000718 QualType ResultType,
719 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000720 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000721 bool IsClassMessage,
722 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000723 llvm::Value *Arg0 =
724 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000725 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000726 Arg0, CGF.getContext().getObjCIdType(),
727 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000728}
729
730CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000731 QualType ResultType,
732 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000733 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000734 QualType Arg0Ty,
735 bool IsSuper,
736 const CallArgList &CallArgs) {
737 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000738 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
739 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
740 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000741 CGF.getContext().getObjCSelType()));
742 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000743
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000744 const llvm::FunctionType *FTy =
745 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
746 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000747
748 llvm::Constant *Fn;
749 if (CGM.ReturnTypeUsesSret(ResultType)) {
750 Fn = ObjCTypes.getSendStretFn(IsSuper);
751 } else if (ResultType->isFloatingType()) {
752 // FIXME: Sadly, this is wrong. This actually depends on the
753 // architecture. This happens to be right for x86-32 though.
754 Fn = ObjCTypes.getSendFpretFn(IsSuper);
755 } else {
756 Fn = ObjCTypes.getSendFn(IsSuper);
757 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000758 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000759 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000760}
761
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000762llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000763 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000764 // FIXME: I don't understand why gcc generates this, or where it is
765 // resolved. Investigate. Its also wasteful to look this up over and
766 // over.
767 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
768
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000769 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
770 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000771}
772
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000773void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
774 // FIXME: We shouldn't need this, the protocol decl should contain
775 // enough information to tell us whether this was a declaration or a
776 // definition.
777 DefinedProtocols.insert(PD->getIdentifier());
778
779 // If we have generated a forward reference to this protocol, emit
780 // it now. Otherwise do nothing, the protocol objects are lazily
781 // emitted.
782 if (Protocols.count(PD->getIdentifier()))
783 GetOrEmitProtocol(PD);
784}
785
786llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
787 if (DefinedProtocols.count(PD->getIdentifier()))
788 return GetOrEmitProtocol(PD);
789 return GetOrEmitProtocolRef(PD);
790}
791
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000792/*
793 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
794 struct _objc_protocol {
795 struct _objc_protocol_extension *isa;
796 char *protocol_name;
797 struct _objc_protocol_list *protocol_list;
798 struct _objc__method_prototype_list *instance_methods;
799 struct _objc__method_prototype_list *class_methods
800 };
801
802 See EmitProtocolExtension().
803*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000804llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
805 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
806
807 // Early exit if a defining object has already been generated.
808 if (Entry && Entry->hasInitializer())
809 return Entry;
810
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000811 // FIXME: I don't understand why gcc generates this, or where it is
812 // resolved. Investigate. Its also wasteful to look this up over and
813 // over.
814 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
815
Chris Lattner8ec03f52008-11-24 03:54:41 +0000816 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000817
818 // Construct method lists.
819 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
820 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
821 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
822 e = PD->instmeth_end(); i != e; ++i) {
823 ObjCMethodDecl *MD = *i;
824 llvm::Constant *C = GetMethodDescriptionConstant(MD);
825 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
826 OptInstanceMethods.push_back(C);
827 } else {
828 InstanceMethods.push_back(C);
829 }
830 }
831
832 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
833 e = PD->classmeth_end(); i != e; ++i) {
834 ObjCMethodDecl *MD = *i;
835 llvm::Constant *C = GetMethodDescriptionConstant(MD);
836 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
837 OptClassMethods.push_back(C);
838 } else {
839 ClassMethods.push_back(C);
840 }
841 }
842
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000843 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000844 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000845 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000846 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000847 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000848 PD->protocol_begin(),
849 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000850 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000851 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
852 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000853 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
854 InstanceMethods);
855 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000856 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
857 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000858 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
859 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000860 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
861 Values);
862
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000863 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000864 // Already created, fix the linkage and update the initializer.
865 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000866 Entry->setInitializer(Init);
867 } else {
868 Entry =
869 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
870 llvm::GlobalValue::InternalLinkage,
871 Init,
872 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
873 &CGM.getModule());
874 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
875 UsedGlobals.push_back(Entry);
876 // FIXME: Is this necessary? Why only for protocol?
877 Entry->setAlignment(4);
878 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000879
880 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000881}
882
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000883llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000884 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
885
886 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000887 // We use the initializer as a marker of whether this is a forward
888 // reference or not. At module finalization we add the empty
889 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000890 Entry =
891 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000892 llvm::GlobalValue::ExternalLinkage,
893 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000894 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000895 &CGM.getModule());
896 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
897 UsedGlobals.push_back(Entry);
898 // FIXME: Is this necessary? Why only for protocol?
899 Entry->setAlignment(4);
900 }
901
902 return Entry;
903}
904
905/*
906 struct _objc_protocol_extension {
907 uint32_t size;
908 struct objc_method_description_list *optional_instance_methods;
909 struct objc_method_description_list *optional_class_methods;
910 struct objc_property_list *instance_properties;
911 };
912*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000913llvm::Constant *
914CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
915 const ConstantVector &OptInstanceMethods,
916 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000917 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000918 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000919 std::vector<llvm::Constant*> Values(4);
920 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000921 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000922 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
923 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000924 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
925 OptInstanceMethods);
926 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000927 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
928 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000929 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
930 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000931 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
932 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000933 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000934
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000935 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000936 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
937 Values[3]->isNullValue())
938 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
939
940 llvm::Constant *Init =
941 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
942 llvm::GlobalVariable *GV =
943 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
944 llvm::GlobalValue::InternalLinkage,
945 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000946 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000947 &CGM.getModule());
948 // No special section, but goes in llvm.used
949 UsedGlobals.push_back(GV);
950
951 return GV;
952}
953
954/*
955 struct objc_protocol_list {
956 struct objc_protocol_list *next;
957 long count;
958 Protocol *list[];
959 };
960*/
Daniel Dunbardbc93372008-08-21 21:57:41 +0000961llvm::Constant *
962CGObjCMac::EmitProtocolList(const std::string &Name,
963 ObjCProtocolDecl::protocol_iterator begin,
964 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000965 std::vector<llvm::Constant*> ProtocolRefs;
966
Daniel Dunbardbc93372008-08-21 21:57:41 +0000967 for (; begin != end; ++begin)
968 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969
970 // Just return null for empty protocol lists
971 if (ProtocolRefs.empty())
972 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
973
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000974 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000975 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
976
977 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000978 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000979 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
980 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
981 Values[2] =
982 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
983 ProtocolRefs.size()),
984 ProtocolRefs);
985
986 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
987 llvm::GlobalVariable *GV =
988 new llvm::GlobalVariable(Init->getType(), false,
989 llvm::GlobalValue::InternalLinkage,
990 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +0000991 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000992 &CGM.getModule());
993 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
994 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
995}
996
997/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000998 struct _objc_property {
999 const char * const name;
1000 const char * const attributes;
1001 };
1002
1003 struct _objc_property_list {
1004 uint32_t entsize; // sizeof (struct _objc_property)
1005 uint32_t prop_count;
1006 struct _objc_property[prop_count];
1007 };
1008*/
1009llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001010 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +00001011 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001012 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001013 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1014 E = OCD->prop_end(); I != E; ++I) {
1015 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001016 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001017 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001018 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1019 Prop));
1020 }
1021
1022 // Return null for empty list.
1023 if (Properties.empty())
1024 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1025
1026 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001027 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001028 std::vector<llvm::Constant*> Values(3);
1029 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1030 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1031 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1032 Properties.size());
1033 Values[2] = llvm::ConstantArray::get(AT, Properties);
1034 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1035
1036 llvm::GlobalVariable *GV =
1037 new llvm::GlobalVariable(Init->getType(), false,
1038 llvm::GlobalValue::InternalLinkage,
1039 Init,
1040 Name,
1041 &CGM.getModule());
1042 // No special section on property lists?
1043 UsedGlobals.push_back(GV);
1044 return llvm::ConstantExpr::getBitCast(GV,
1045 ObjCTypes.PropertyListPtrTy);
1046
1047}
1048
1049/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001050 struct objc_method_description_list {
1051 int count;
1052 struct objc_method_description list[];
1053 };
1054*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001055llvm::Constant *
1056CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1057 std::vector<llvm::Constant*> Desc(2);
1058 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1059 ObjCTypes.SelectorPtrTy);
1060 Desc[1] = GetMethodVarType(MD);
1061 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1062 Desc);
1063}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001064
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001065llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1066 const char *Section,
1067 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001068 // Return null for empty list.
1069 if (Methods.empty())
1070 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1071
1072 std::vector<llvm::Constant*> Values(2);
1073 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1074 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1075 Methods.size());
1076 Values[1] = llvm::ConstantArray::get(AT, Methods);
1077 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1078
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001079 llvm::GlobalVariable *GV =
1080 new llvm::GlobalVariable(Init->getType(), false,
1081 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001082 Init, Name, &CGM.getModule());
1083 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001084 UsedGlobals.push_back(GV);
1085 return llvm::ConstantExpr::getBitCast(GV,
1086 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001087}
1088
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001089/*
1090 struct _objc_category {
1091 char *category_name;
1092 char *class_name;
1093 struct _objc_method_list *instance_methods;
1094 struct _objc_method_list *class_methods;
1095 struct _objc_protocol_list *protocols;
1096 uint32_t size; // <rdar://4585769>
1097 struct _objc_property_list *instance_properties;
1098 };
1099 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001100void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001101 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001102
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001103 // FIXME: This is poor design, the OCD should have a pointer to the
1104 // category decl. Additionally, note that Category can be null for
1105 // the @implementation w/o an @interface case. Sema should just
1106 // create one for us as it does for @implementation so everyone else
1107 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001108 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001109 const ObjCCategoryDecl *Category =
1110 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001111 std::string ExtName(Interface->getNameAsString() + "_" +
1112 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001113
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001114 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1115 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1116 e = OCD->instmeth_end(); i != e; ++i) {
1117 // Instance methods should always be defined.
1118 InstanceMethods.push_back(GetMethodConstant(*i));
1119 }
1120 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1121 e = OCD->classmeth_end(); i != e; ++i) {
1122 // Class methods should always be defined.
1123 ClassMethods.push_back(GetMethodConstant(*i));
1124 }
1125
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001126 std::vector<llvm::Constant*> Values(7);
1127 Values[0] = GetClassName(OCD->getIdentifier());
1128 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001129 Values[2] =
1130 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1131 ExtName,
1132 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001133 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001134 Values[3] =
1135 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1136 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001137 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001138 if (Category) {
1139 Values[4] =
1140 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1141 Category->protocol_begin(),
1142 Category->protocol_end());
1143 } else {
1144 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1145 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001146 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001147
1148 // If there is no category @interface then there can be no properties.
1149 if (Category) {
1150 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001151 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001152 } else {
1153 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1154 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001155
1156 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1157 Values);
1158
1159 llvm::GlobalVariable *GV =
1160 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1161 llvm::GlobalValue::InternalLinkage,
1162 Init,
1163 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1164 &CGM.getModule());
1165 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1166 UsedGlobals.push_back(GV);
1167 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001168}
1169
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001170// FIXME: Get from somewhere?
1171enum ClassFlags {
1172 eClassFlags_Factory = 0x00001,
1173 eClassFlags_Meta = 0x00002,
1174 // <rdr://5142207>
1175 eClassFlags_HasCXXStructors = 0x02000,
1176 eClassFlags_Hidden = 0x20000,
1177 eClassFlags_ABI2_Hidden = 0x00010,
1178 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1179};
1180
1181// <rdr://5142207&4705298&4843145>
1182static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1183 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1184 // FIXME: Support -fvisibility
1185 switch (attr->getVisibility()) {
1186 default:
1187 assert(0 && "Unknown visibility");
1188 return false;
1189 case VisibilityAttr::DefaultVisibility:
1190 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1191 return false;
1192 case VisibilityAttr::HiddenVisibility:
1193 return true;
1194 }
1195 } else {
1196 return false; // FIXME: Support -fvisibility
1197 }
1198}
1199
1200/*
1201 struct _objc_class {
1202 Class isa;
1203 Class super_class;
1204 const char *name;
1205 long version;
1206 long info;
1207 long instance_size;
1208 struct _objc_ivar_list *ivars;
1209 struct _objc_method_list *methods;
1210 struct _objc_cache *cache;
1211 struct _objc_protocol_list *protocols;
1212 // Objective-C 1.0 extensions (<rdr://4585769>)
1213 const char *ivar_layout;
1214 struct _objc_class_ext *ext;
1215 };
1216
1217 See EmitClassExtension();
1218 */
1219void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001220 DefinedSymbols.insert(ID->getIdentifier());
1221
Chris Lattner8ec03f52008-11-24 03:54:41 +00001222 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001223 // FIXME: Gross
1224 ObjCInterfaceDecl *Interface =
1225 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001226 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001227 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001228 Interface->protocol_begin(),
1229 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001230 const llvm::Type *InterfaceTy =
1231 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1232 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001233 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001234
1235 // FIXME: Set CXX-structors flag.
1236 if (IsClassHidden(ID->getClassInterface()))
1237 Flags |= eClassFlags_Hidden;
1238
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001239 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1240 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1241 e = ID->instmeth_end(); i != e; ++i) {
1242 // Instance methods should always be defined.
1243 InstanceMethods.push_back(GetMethodConstant(*i));
1244 }
1245 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1246 e = ID->classmeth_end(); i != e; ++i) {
1247 // Class methods should always be defined.
1248 ClassMethods.push_back(GetMethodConstant(*i));
1249 }
1250
1251 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1252 e = ID->propimpl_end(); i != e; ++i) {
1253 ObjCPropertyImplDecl *PID = *i;
1254
1255 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1256 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1257
1258 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1259 if (llvm::Constant *C = GetMethodConstant(MD))
1260 InstanceMethods.push_back(C);
1261 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1262 if (llvm::Constant *C = GetMethodConstant(MD))
1263 InstanceMethods.push_back(C);
1264 }
1265 }
1266
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001267 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001268 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001269 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001270 // Record a reference to the super class.
1271 LazySymbols.insert(Super->getIdentifier());
1272
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001273 Values[ 1] =
1274 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1275 ObjCTypes.ClassPtrTy);
1276 } else {
1277 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1278 }
1279 Values[ 2] = GetClassName(ID->getIdentifier());
1280 // Version is always 0.
1281 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1282 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1283 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1284 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001285 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001286 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001287 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001288 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001289 // cache is always NULL.
1290 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1291 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001292 // FIXME: Set ivar_layout
1293 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001294 Values[11] = EmitClassExtension(ID);
1295 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1296 Values);
1297
1298 llvm::GlobalVariable *GV =
1299 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1300 llvm::GlobalValue::InternalLinkage,
1301 Init,
1302 std::string("\01L_OBJC_CLASS_")+ClassName,
1303 &CGM.getModule());
1304 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1305 UsedGlobals.push_back(GV);
1306 // FIXME: Why?
1307 GV->setAlignment(32);
1308 DefinedClasses.push_back(GV);
1309}
1310
1311llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1312 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001313 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001314 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001315 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001316 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001317
1318 if (IsClassHidden(ID->getClassInterface()))
1319 Flags |= eClassFlags_Hidden;
1320
1321 std::vector<llvm::Constant*> Values(12);
1322 // The isa for the metaclass is the root of the hierarchy.
1323 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1324 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1325 Root = Super;
1326 Values[ 0] =
1327 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1328 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001329 // The super class for the metaclass is emitted as the name of the
1330 // super class. The runtime fixes this up to point to the
1331 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001332 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1333 Values[ 1] =
1334 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1335 ObjCTypes.ClassPtrTy);
1336 } else {
1337 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1338 }
1339 Values[ 2] = GetClassName(ID->getIdentifier());
1340 // Version is always 0.
1341 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1342 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1343 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1344 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001345 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001346 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001347 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001348 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001349 // cache is always NULL.
1350 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1351 Values[ 9] = Protocols;
1352 // ivar_layout for metaclass is always NULL.
1353 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1354 // The class extension is always unused for metaclasses.
1355 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1356 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1357 Values);
1358
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001359 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001360 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001361
1362 // Check for a forward reference.
1363 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1364 if (GV) {
1365 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1366 "Forward metaclass reference has incorrect type.");
1367 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1368 GV->setInitializer(Init);
1369 } else {
1370 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1371 llvm::GlobalValue::InternalLinkage,
1372 Init, Name,
1373 &CGM.getModule());
1374 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001375 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1376 UsedGlobals.push_back(GV);
1377 // FIXME: Why?
1378 GV->setAlignment(32);
1379
1380 return GV;
1381}
1382
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001383llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001384 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001385
1386 // FIXME: Should we look these up somewhere other than the
1387 // module. Its a bit silly since we only generate these while
1388 // processing an implementation, so exactly one pointer would work
1389 // if know when we entered/exitted an implementation block.
1390
1391 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001392 // Previously, metaclass with internal linkage may have been defined.
1393 // pass 'true' as 2nd argument so it is returned.
1394 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001395 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1396 "Forward metaclass reference has incorrect type.");
1397 return GV;
1398 } else {
1399 // Generate as an external reference to keep a consistent
1400 // module. This will be patched up when we emit the metaclass.
1401 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1402 llvm::GlobalValue::ExternalLinkage,
1403 0,
1404 Name,
1405 &CGM.getModule());
1406 }
1407}
1408
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001409/*
1410 struct objc_class_ext {
1411 uint32_t size;
1412 const char *weak_ivar_layout;
1413 struct _objc_property_list *properties;
1414 };
1415*/
1416llvm::Constant *
1417CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1418 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001419 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001420
1421 std::vector<llvm::Constant*> Values(3);
1422 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001423 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001424 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001425 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001426 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001427
1428 // Return null if no extension bits are used.
1429 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1430 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1431
1432 llvm::Constant *Init =
1433 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1434 llvm::GlobalVariable *GV =
1435 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1436 llvm::GlobalValue::InternalLinkage,
1437 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001438 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001439 &CGM.getModule());
1440 // No special section, but goes in llvm.used
1441 UsedGlobals.push_back(GV);
1442
1443 return GV;
1444}
1445
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001446/// countInheritedIvars - count number of ivars in class and its super class(s)
1447///
1448static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1449 int count = 0;
1450 if (!OI)
1451 return 0;
1452 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1453 if (SuperClass)
1454 count += countInheritedIvars(SuperClass);
1455 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1456 E = OI->ivar_end(); I != E; ++I)
1457 ++count;
1458 return count;
1459}
1460
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001461/*
1462 struct objc_ivar {
1463 char *ivar_name;
1464 char *ivar_type;
1465 int ivar_offset;
1466 };
1467
1468 struct objc_ivar_list {
1469 int ivar_count;
1470 struct objc_ivar list[count];
1471 };
1472 */
1473llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1474 bool ForClass,
1475 const llvm::Type *InterfaceTy) {
1476 std::vector<llvm::Constant*> Ivars, Ivar(3);
1477
1478 // When emitting the root class GCC emits ivar entries for the
1479 // actual class structure. It is not clear if we need to follow this
1480 // behavior; for now lets try and get away with not doing it. If so,
1481 // the cleanest solution would be to make up an ObjCInterfaceDecl
1482 // for the class.
1483 if (ForClass)
1484 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1485
1486 const llvm::StructLayout *Layout =
1487 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001488 ObjCInterfaceDecl *OID =
1489 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1490 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1491 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1492 RecordDecl::field_iterator ifield = RD->field_begin();
1493 while (countSuperClassIvars-- > 0)
1494 ++ifield;
1495 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1496 FieldDecl *Field = *ifield;
1497 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1498 getLLVMFieldNo(Field));
1499 if (Field->getIdentifier())
1500 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1501 else
1502 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001503 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001504 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001505 Ivar[1] = GetMethodVarType(TypeStr);
1506 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001507 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001508 }
1509
1510 // Return null for empty list.
1511 if (Ivars.empty())
1512 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1513
1514 std::vector<llvm::Constant*> Values(2);
1515 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1516 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1517 Ivars.size());
1518 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1519 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1520
1521 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1522 "\01L_OBJC_INSTANCE_VARIABLES_");
1523 llvm::GlobalVariable *GV =
1524 new llvm::GlobalVariable(Init->getType(), false,
1525 llvm::GlobalValue::InternalLinkage,
1526 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001527 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001528 &CGM.getModule());
1529 if (ForClass) {
1530 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1531 // FIXME: Why is this only here?
1532 GV->setAlignment(32);
1533 } else {
1534 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1535 }
1536 UsedGlobals.push_back(GV);
1537 return llvm::ConstantExpr::getBitCast(GV,
1538 ObjCTypes.IvarListPtrTy);
1539}
1540
1541/*
1542 struct objc_method {
1543 SEL method_name;
1544 char *method_types;
1545 void *method;
1546 };
1547
1548 struct objc_method_list {
1549 struct objc_method_list *obsolete;
1550 int count;
1551 struct objc_method methods_list[count];
1552 };
1553*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001554
1555/// GetMethodConstant - Return a struct objc_method constant for the
1556/// given method if it has been defined. The result is null if the
1557/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001558llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001559 // FIXME: Use DenseMap::lookup
1560 llvm::Function *Fn = MethodDefinitions[MD];
1561 if (!Fn)
1562 return 0;
1563
1564 std::vector<llvm::Constant*> Method(3);
1565 Method[0] =
1566 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1567 ObjCTypes.SelectorPtrTy);
1568 Method[1] = GetMethodVarType(MD);
1569 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1570 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1571}
1572
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001573llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1574 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001575 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001576 // Return null for empty list.
1577 if (Methods.empty())
1578 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1579
1580 std::vector<llvm::Constant*> Values(3);
1581 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1582 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1583 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1584 Methods.size());
1585 Values[2] = llvm::ConstantArray::get(AT, Methods);
1586 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1587
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001588 llvm::GlobalVariable *GV =
1589 new llvm::GlobalVariable(Init->getType(), false,
1590 llvm::GlobalValue::InternalLinkage,
1591 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001592 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001593 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001594 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001595 UsedGlobals.push_back(GV);
1596 return llvm::ConstantExpr::getBitCast(GV,
1597 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001598}
1599
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001600llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD,
1601 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001602 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001603 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001604
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001605 const llvm::FunctionType *MethodTy =
1606 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001607 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001608 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001609 llvm::GlobalValue::InternalLinkage,
1610 Name,
1611 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001612 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001613
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001614 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001615}
1616
1617llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001618 // Abuse this interface function as a place to finalize.
1619 FinishModule();
1620
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001621 return NULL;
1622}
1623
Daniel Dunbar49f66022008-09-24 03:38:44 +00001624llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1625 return ObjCTypes.GetPropertyFn;
1626}
1627
1628llvm::Function *CGObjCMac::GetPropertySetFunction() {
1629 return ObjCTypes.SetPropertyFn;
1630}
1631
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001632llvm::Function *CGObjCMac::EnumerationMutationFunction()
1633{
1634 return ObjCTypes.EnumerationMutationFn;
1635}
1636
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001637/*
1638
1639Objective-C setjmp-longjmp (sjlj) Exception Handling
1640--
1641
1642The basic framework for a @try-catch-finally is as follows:
1643{
1644 objc_exception_data d;
1645 id _rethrow = null;
1646
1647 objc_exception_try_enter(&d);
1648 if (!setjmp(d.jmp_buf)) {
1649 ... try body ...
1650 } else {
1651 // exception path
1652 id _caught = objc_exception_extract(&d);
1653
1654 // enter new try scope for handlers
1655 if (!setjmp(d.jmp_buf)) {
1656 ... match exception and execute catch blocks ...
1657
1658 // fell off end, rethrow.
1659 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001660 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001661 } else {
1662 // exception in catch block
1663 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001664 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001665 }
1666 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001667 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001668
1669finally:
1670 // match either the initial try_enter or the catch try_enter,
1671 // depending on the path followed.
1672 objc_exception_try_exit(&d);
1673finally_no_exit:
1674 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001675 ... dispatch to finally destination ...
1676
1677finally_rethrow:
1678 objc_exception_throw(_rethrow);
1679
1680finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001681}
1682
1683This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001684uses _rethrow to determine if objc_exception_try_exit should be called
1685and if the object should be rethrown. This breaks in the face of
1686throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001687
1688We specialize this framework for a few particular circumstances:
1689
1690 - If there are no catch blocks, then we avoid emitting the second
1691 exception handling context.
1692
1693 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1694 e)) we avoid emitting the code to rethrow an uncaught exception.
1695
1696 - FIXME: If there is no @finally block we can do a few more
1697 simplifications.
1698
1699Rethrows and Jumps-Through-Finally
1700--
1701
1702Support for implicit rethrows and jumping through the finally block is
1703handled by storing the current exception-handling context in
1704ObjCEHStack.
1705
Daniel Dunbar898d5082008-09-30 01:06:03 +00001706In order to implement proper @finally semantics, we support one basic
1707mechanism for jumping through the finally block to an arbitrary
1708destination. Constructs which generate exits from a @try or @catch
1709block use this mechanism to implement the proper semantics by chaining
1710jumps, as necessary.
1711
1712This mechanism works like the one used for indirect goto: we
1713arbitrarily assign an ID to each destination and store the ID for the
1714destination in a variable prior to entering the finally block. At the
1715end of the finally block we simply create a switch to the proper
1716destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001717
1718Code gen for @synchronized(expr) stmt;
1719Effectively generating code for:
1720objc_sync_enter(expr);
1721@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001722*/
1723
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001724void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1725 const Stmt &S) {
1726 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001727 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001728 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1729 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1730 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1731 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001732 llvm::Value *DestCode =
1733 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1734
1735 // Generate jump code. Done here so we can directly add things to
1736 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001737 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001738 llvm::SwitchInst *FinallySwitch =
1739 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1740 FinallyEnd, 10, FinallyJump);
1741
1742 // Push an EH context entry, used for handling rethrows and jumps
1743 // through finally.
1744 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1745 FinallySwitch, DestCode);
1746 CGF.ObjCEHStack.push_back(&EHEntry);
1747
1748 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001749 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1750 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001751 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1752 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001753 if (!isTry) {
1754 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001755 llvm::Value *Arg = CGF.EmitScalarExpr(
1756 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1757 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1758 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001759 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001760
1761 // Enter a new try block and call setjmp.
1762 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1763 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1764 "jmpbufarray");
1765 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1766 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1767 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001768
Daniel Dunbar55e87422008-11-11 02:29:29 +00001769 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1770 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001771 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001772 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001773
1774 // Emit the @try block.
1775 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001776 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1777 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001778 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001779
1780 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001781 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001782
1783 // Retrieve the exception object. We may emit multiple blocks but
1784 // nothing can cross this so the value is already in SSA form.
1785 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1786 ExceptionData,
1787 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001788 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001789 if (!isTry)
1790 {
1791 CGF.Builder.CreateStore(Caught, RethrowPtr);
1792 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1793 }
1794 else if (const ObjCAtCatchStmt* CatchStmt =
1795 cast<ObjCAtTryStmt>(S).getCatchStmts())
1796 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001797 // Enter a new exception try block (in case a @catch block throws
1798 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001799 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001800
Anders Carlsson80f25672008-09-09 17:59:25 +00001801 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1802 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001803 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001804
Daniel Dunbar55e87422008-11-11 02:29:29 +00001805 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1806 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001807 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001808
1809 CGF.EmitBlock(CatchBlock);
1810
Daniel Dunbar55e40722008-09-27 07:03:52 +00001811 // Handle catch list. As a special case we check if everything is
1812 // matched and avoid generating code for falling off the end if
1813 // so.
1814 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001815 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001816 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001817
Anders Carlssondde0a942008-09-11 09:15:33 +00001818 const DeclStmt *CatchParam =
1819 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001820 const VarDecl *VD = 0;
1821 const PointerType *PT = 0;
1822
Anders Carlsson80f25672008-09-09 17:59:25 +00001823 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001824 if (!CatchParam) {
1825 AllMatched = true;
1826 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001827 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001828 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001829
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001830 // catch(id e) always matches.
1831 // FIXME: For the time being we also match id<X>; this should
1832 // be rejected by Sema instead.
1833 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1834 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001835 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001836 }
1837
Daniel Dunbar55e40722008-09-27 07:03:52 +00001838 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001839 if (CatchParam) {
1840 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001841 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001842 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001843 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001844
Anders Carlssondde0a942008-09-11 09:15:33 +00001845 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001846 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001847 break;
1848 }
1849
Daniel Dunbar129271a2008-09-27 07:36:24 +00001850 assert(PT && "Unexpected non-pointer type in @catch");
1851 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001852 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001853 assert(ObjCType && "Catch parameter must have Objective-C type!");
1854
1855 // Check if the @catch block matches the exception object.
1856 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1857
Anders Carlsson80f25672008-09-09 17:59:25 +00001858 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1859 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001860
Daniel Dunbar55e87422008-11-11 02:29:29 +00001861 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001862
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001863 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001864 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001865
1866 // Emit the @catch block.
1867 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001868 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001869 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001870
1871 llvm::Value *Tmp =
1872 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1873 "tmp");
1874 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001875
1876 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001877 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001878
1879 CGF.EmitBlock(NextCatchBlock);
1880 }
1881
Daniel Dunbar55e40722008-09-27 07:03:52 +00001882 if (!AllMatched) {
1883 // None of the handlers caught the exception, so store it to be
1884 // rethrown at the end of the @finally block.
1885 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001886 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001887 }
1888
1889 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001890 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001891 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1892 ExceptionData),
1893 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001894 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001895 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001896 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001897 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001898 }
1899
Daniel Dunbar898d5082008-09-30 01:06:03 +00001900 // Pop the exception-handling stack entry. It is important to do
1901 // this now, because the code in the @finally block is not in this
1902 // context.
1903 CGF.ObjCEHStack.pop_back();
1904
Anders Carlsson80f25672008-09-09 17:59:25 +00001905 // Emit the @finally block.
1906 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001907 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001908
1909 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001910 if (isTry) {
1911 if (const ObjCAtFinallyStmt* FinallyStmt =
1912 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1913 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1914 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001915 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001916 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001917 // For @synchronized, call objc_sync_enter(sync.expr)
1918 llvm::Value *Arg = CGF.EmitScalarExpr(
1919 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1920 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1921 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1922 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001923
Daniel Dunbar898d5082008-09-30 01:06:03 +00001924 CGF.EmitBlock(FinallyJump);
1925
1926 CGF.EmitBlock(FinallyRethrow);
1927 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1928 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001929 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001930
1931 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001932}
1933
1934void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001935 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001936 llvm::Value *ExceptionAsObject;
1937
1938 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1939 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1940 ExceptionAsObject =
1941 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1942 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001943 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001944 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001945 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001946 }
1947
1948 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001949 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001950
1951 // Clear the insertion point to indicate we are in unreachable code.
1952 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001953}
1954
Daniel Dunbar898d5082008-09-30 01:06:03 +00001955void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1956 llvm::BasicBlock *Dst,
1957 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001958 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001959 return;
1960
1961 // Find the destination code for this block. We always use 0 for the
1962 // fallthrough block (default destination).
1963 llvm::SwitchInst *SI = E->FinallySwitch;
1964 llvm::ConstantInt *ID;
1965 if (Dst == SI->getDefaultDest()) {
1966 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1967 } else {
1968 ID = SI->findCaseDest(Dst);
1969 if (!ID) {
1970 // No code found, get a new unique one by just using the number
1971 // of switch successors.
1972 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1973 SI->addCase(ID, Dst);
1974 }
1975 }
1976
1977 // Set the destination code and branch.
1978 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001979 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001980}
1981
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001982/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001983/// object: objc_read_weak (id *src)
1984///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001985llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001986 llvm::Value *AddrWeakObj)
1987{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001988 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001989 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001990 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001991 return read_weak;
1992}
1993
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001994/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
1995/// objc_assign_weak (id src, id *dst)
1996///
1997void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1998 llvm::Value *src, llvm::Value *dst)
1999{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002000 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2001 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002002 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2003 src, dst, "weakassign");
2004 return;
2005}
2006
Fariborz Jahanian58626502008-11-19 00:59:10 +00002007/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2008/// objc_assign_global (id src, id *dst)
2009///
2010void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2011 llvm::Value *src, llvm::Value *dst)
2012{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002013 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2014 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002015 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2016 src, dst, "globalassign");
2017 return;
2018}
2019
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002020/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2021/// objc_assign_ivar (id src, id *dst)
2022///
2023void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2024 llvm::Value *src, llvm::Value *dst)
2025{
2026 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2027 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2028 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2029 src, dst, "assignivar");
2030 return;
2031}
2032
Fariborz Jahanian58626502008-11-19 00:59:10 +00002033/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2034/// objc_assign_strongCast (id src, id *dst)
2035///
2036void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2037 llvm::Value *src, llvm::Value *dst)
2038{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002039 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2040 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002041 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2042 src, dst, "weakassign");
2043 return;
2044}
2045
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002046/* *** Private Interface *** */
2047
2048/// EmitImageInfo - Emit the image info marker used to encode some module
2049/// level information.
2050///
2051/// See: <rdr://4810609&4810587&4810587>
2052/// struct IMAGE_INFO {
2053/// unsigned version;
2054/// unsigned flags;
2055/// };
2056enum ImageInfoFlags {
2057 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2058 eImageInfo_GarbageCollected = (1 << 1),
2059 eImageInfo_GCOnly = (1 << 2)
2060};
2061
2062void CGObjCMac::EmitImageInfo() {
2063 unsigned version = 0; // Version is unused?
2064 unsigned flags = 0;
2065
2066 // FIXME: Fix and continue?
2067 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2068 flags |= eImageInfo_GarbageCollected;
2069 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2070 flags |= eImageInfo_GCOnly;
2071
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002072 // Emitted as int[2];
2073 llvm::Constant *values[2] = {
2074 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2075 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2076 };
2077 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002078 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002079 new llvm::GlobalVariable(AT, true,
2080 llvm::GlobalValue::InternalLinkage,
2081 llvm::ConstantArray::get(AT, values, 2),
2082 "\01L_OBJC_IMAGE_INFO",
2083 &CGM.getModule());
2084
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002085 if (ObjCABI == 1) {
2086 GV->setSection("__OBJC, __image_info,regular");
2087 } else {
2088 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2089 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002090
2091 UsedGlobals.push_back(GV);
2092}
2093
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002094
2095// struct objc_module {
2096// unsigned long version;
2097// unsigned long size;
2098// const char *name;
2099// Symtab symtab;
2100// };
2101
2102// FIXME: Get from somewhere
2103static const int ModuleVersion = 7;
2104
2105void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002106 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002107
2108 std::vector<llvm::Constant*> Values(4);
2109 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2110 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002111 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002112 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002113 Values[3] = EmitModuleSymbols();
2114
2115 llvm::GlobalVariable *GV =
2116 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2117 llvm::GlobalValue::InternalLinkage,
2118 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2119 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002120 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002121 &CGM.getModule());
2122 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2123 UsedGlobals.push_back(GV);
2124}
2125
2126llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002127 unsigned NumClasses = DefinedClasses.size();
2128 unsigned NumCategories = DefinedCategories.size();
2129
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002130 // Return null if no symbols were defined.
2131 if (!NumClasses && !NumCategories)
2132 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2133
2134 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002135 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2136 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2137 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2138 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2139
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002140 // The runtime expects exactly the list of defined classes followed
2141 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002142 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002143 for (unsigned i=0; i<NumClasses; i++)
2144 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2145 ObjCTypes.Int8PtrTy);
2146 for (unsigned i=0; i<NumCategories; i++)
2147 Symbols[NumClasses + i] =
2148 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2149 ObjCTypes.Int8PtrTy);
2150
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002151 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002152 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 NumClasses + NumCategories),
2154 Symbols);
2155
2156 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2157
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002158 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002159 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002160 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002162 "\01L_OBJC_SYMBOLS",
2163 &CGM.getModule());
2164 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2165 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2167}
2168
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002169llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002170 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002171 LazySymbols.insert(ID->getIdentifier());
2172
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2174
2175 if (!Entry) {
2176 llvm::Constant *Casted =
2177 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2178 ObjCTypes.ClassPtrTy);
2179 Entry =
2180 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2181 llvm::GlobalValue::InternalLinkage,
2182 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2183 &CGM.getModule());
2184 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2185 UsedGlobals.push_back(Entry);
2186 }
2187
2188 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002189}
2190
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002191llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002192 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2193
2194 if (!Entry) {
2195 llvm::Constant *Casted =
2196 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2197 ObjCTypes.SelectorPtrTy);
2198 Entry =
2199 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2200 llvm::GlobalValue::InternalLinkage,
2201 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2202 &CGM.getModule());
2203 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2204 UsedGlobals.push_back(Entry);
2205 }
2206
2207 return Builder.CreateLoad(Entry, false, "tmp");
2208}
2209
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002210llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
2211 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002212
2213 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002214 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002215 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002216 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002217 llvm::GlobalValue::InternalLinkage,
2218 C, "\01L_OBJC_CLASS_NAME_",
2219 &CGM.getModule());
2220 Entry->setSection("__TEXT,__cstring,cstring_literals");
2221 UsedGlobals.push_back(Entry);
2222 }
2223
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002224 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002225}
2226
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002227llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002228 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2229
2230 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002231 // FIXME: Avoid std::string copying.
2232 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002233 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002234 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002235 llvm::GlobalValue::InternalLinkage,
2236 C, "\01L_OBJC_METH_VAR_NAME_",
2237 &CGM.getModule());
2238 Entry->setSection("__TEXT,__cstring,cstring_literals");
2239 UsedGlobals.push_back(Entry);
2240 }
2241
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002242 return getConstantGEP(Entry, 0, 0);
2243}
2244
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002245// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002246llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2248}
2249
2250// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002251llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002252 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2253}
2254
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002255llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002257
2258 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002259 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002260 Entry =
2261 new llvm::GlobalVariable(C->getType(), false,
2262 llvm::GlobalValue::InternalLinkage,
2263 C, "\01L_OBJC_METH_VAR_TYPE_",
2264 &CGM.getModule());
2265 Entry->setSection("__TEXT,__cstring,cstring_literals");
2266 UsedGlobals.push_back(Entry);
2267 }
2268
2269 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002270}
2271
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002272// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002273llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002275 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2276 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 return GetMethodVarType(TypeStr);
2278}
2279
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002280// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002281llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002282 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2283
2284 if (!Entry) {
2285 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2286 Entry =
2287 new llvm::GlobalVariable(C->getType(), false,
2288 llvm::GlobalValue::InternalLinkage,
2289 C, "\01L_OBJC_PROP_NAME_ATTR_",
2290 &CGM.getModule());
2291 Entry->setSection("__TEXT,__cstring,cstring_literals");
2292 UsedGlobals.push_back(Entry);
2293 }
2294
2295 return getConstantGEP(Entry, 0, 0);
2296}
2297
2298// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002299// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002300llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002301 const Decl *Container) {
2302 std::string TypeStr;
2303 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002304 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2305}
2306
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002307void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2308 const ObjCContainerDecl *CD,
2309 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002310 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002311 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002312 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002313 assert (CD && "Missing container decl in GetNameForMethod");
2314 NameOut += CD->getNameAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +00002315 NameOut += ' ';
2316 NameOut += D->getSelector().getAsString();
2317 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002318}
2319
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002320void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002321 EmitModuleInfo();
2322
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002323 // Emit the dummy bodies for any protocols which were referenced but
2324 // never defined.
2325 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2326 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2327 if (i->second->hasInitializer())
2328 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002329
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002330 std::vector<llvm::Constant*> Values(5);
2331 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2332 Values[1] = GetClassName(i->first);
2333 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2334 Values[3] = Values[4] =
2335 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2336 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2337 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2338 Values));
2339 }
2340
2341 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002342 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002343 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002344 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002345 }
2346
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002347 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002348 llvm::GlobalValue *GV =
2349 new llvm::GlobalVariable(AT, false,
2350 llvm::GlobalValue::AppendingLinkage,
2351 llvm::ConstantArray::get(AT, Used),
2352 "llvm.used",
2353 &CGM.getModule());
2354
2355 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002356
2357 // Add assembler directives to add lazy undefined symbol references
2358 // for classes which are referenced but not defined. This is
2359 // important for correct linker interaction.
2360
2361 // FIXME: Uh, this isn't particularly portable.
2362 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002363
2364 if (!CGM.getModule().getModuleInlineAsm().empty())
2365 s << "\n";
2366
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002367 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2368 e = LazySymbols.end(); i != e; ++i) {
2369 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2370 }
2371 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2372 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002373 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002374 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2375 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002376
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002377 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002378}
2379
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002380CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002381 : CGObjCCommonMac(cgm),
2382 ObjCTypes(cgm)
2383{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002384 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002385 ObjCABI = 2;
2386}
2387
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002388/* *** */
2389
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002390ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2391: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002392{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002393 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2394 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002395
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002397 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002398 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002399 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2400
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002401 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002402 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002403 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002404
2405 // FIXME: It would be nice to unify this with the opaque type, so
2406 // that the IR comes out a bit cleaner.
2407 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2408 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002409
2410 // I'm not sure I like this. The implicit coordination is a bit
2411 // gross. We should solve this in a reasonable fashion because this
2412 // is a pretty common task (match some runtime data structure with
2413 // an LLVM data structure).
2414
2415 // FIXME: This is leaked.
2416 // FIXME: Merge with rewriter code?
2417
2418 // struct _objc_super {
2419 // id self;
2420 // Class cls;
2421 // }
2422 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2423 SourceLocation(),
2424 &Ctx.Idents.get("_objc_super"));
2425 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2426 Ctx.getObjCIdType(), 0, false));
2427 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2428 Ctx.getObjCClassType(), 0, false));
2429 RD->completeDefinition(Ctx);
2430
2431 SuperCTy = Ctx.getTagDeclType(RD);
2432 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2433
2434 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002435 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2436
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002437 // struct _prop_t {
2438 // char *name;
2439 // char *attributes;
2440 // }
2441 PropertyTy = llvm::StructType::get(Int8PtrTy,
2442 Int8PtrTy,
2443 NULL);
2444 CGM.getModule().addTypeName("struct._prop_t",
2445 PropertyTy);
2446
2447 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002448 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002449 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002450 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002451 // }
2452 PropertyListTy = llvm::StructType::get(IntTy,
2453 IntTy,
2454 llvm::ArrayType::get(PropertyTy, 0),
2455 NULL);
2456 CGM.getModule().addTypeName("struct._prop_list_t",
2457 PropertyListTy);
2458 // struct _prop_list_t *
2459 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2460
2461 // struct _objc_method {
2462 // SEL _cmd;
2463 // char *method_type;
2464 // char *_imp;
2465 // }
2466 MethodTy = llvm::StructType::get(SelectorPtrTy,
2467 Int8PtrTy,
2468 Int8PtrTy,
2469 NULL);
2470 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002471
2472 // struct _objc_cache *
2473 CacheTy = llvm::OpaqueType::get();
2474 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2475 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002476
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002477 // Property manipulation functions.
2478
2479 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2480 std::vector<const llvm::Type*> Params;
2481 Params.push_back(ObjectPtrTy);
2482 Params.push_back(SelectorPtrTy);
2483 Params.push_back(LongTy);
2484 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2485 GetPropertyFn =
2486 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2487 Params,
2488 false),
2489 "objc_getProperty");
2490
2491 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2492 Params.clear();
2493 Params.push_back(ObjectPtrTy);
2494 Params.push_back(SelectorPtrTy);
2495 Params.push_back(LongTy);
2496 Params.push_back(ObjectPtrTy);
2497 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2498 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2499 SetPropertyFn =
2500 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2501 Params,
2502 false),
2503 "objc_setProperty");
2504 // Enumeration mutation.
2505
2506 Params.clear();
2507 Params.push_back(ObjectPtrTy);
2508 EnumerationMutationFn =
2509 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2510 Params,
2511 false),
2512 "objc_enumerationMutation");
2513
2514 // gc's API
2515 // id objc_read_weak (id *)
2516 Params.clear();
2517 Params.push_back(PtrObjectPtrTy);
2518 GcReadWeakFn =
2519 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2520 Params,
2521 false),
2522 "objc_read_weak");
2523 // id objc_assign_weak (id, id *)
2524 Params.clear();
2525 Params.push_back(ObjectPtrTy);
2526 Params.push_back(PtrObjectPtrTy);
2527 GcAssignWeakFn =
2528 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2529 Params,
2530 false),
2531 "objc_assign_weak");
2532 GcAssignGlobalFn =
2533 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2534 Params,
2535 false),
2536 "objc_assign_global");
2537 GcAssignIvarFn =
2538 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2539 Params,
2540 false),
2541 "objc_assign_ivar");
2542 GcAssignStrongCastFn =
2543 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2544 Params,
2545 false),
2546 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002547}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002548
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002549ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2550 : ObjCCommonTypesHelper(cgm)
2551{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002552 // struct _objc_method_description {
2553 // SEL name;
2554 // char *types;
2555 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002556 MethodDescriptionTy =
2557 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002558 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002559 NULL);
2560 CGM.getModule().addTypeName("struct._objc_method_description",
2561 MethodDescriptionTy);
2562
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002563 // struct _objc_method_description_list {
2564 // int count;
2565 // struct _objc_method_description[1];
2566 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002567 MethodDescriptionListTy =
2568 llvm::StructType::get(IntTy,
2569 llvm::ArrayType::get(MethodDescriptionTy, 0),
2570 NULL);
2571 CGM.getModule().addTypeName("struct._objc_method_description_list",
2572 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002573
2574 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002575 MethodDescriptionListPtrTy =
2576 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2577
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002578 // Protocol description structures
2579
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002580 // struct _objc_protocol_extension {
2581 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2582 // struct _objc_method_description_list *optional_instance_methods;
2583 // struct _objc_method_description_list *optional_class_methods;
2584 // struct _objc_property_list *instance_properties;
2585 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002586 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002587 llvm::StructType::get(IntTy,
2588 MethodDescriptionListPtrTy,
2589 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002590 PropertyListPtrTy,
2591 NULL);
2592 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2593 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002594
2595 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002596 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2597
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002598 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002599
2600 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2601 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2602
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002603 const llvm::Type *T =
2604 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2605 LongTy,
2606 llvm::ArrayType::get(ProtocolTyHolder, 0),
2607 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002608 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2609
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002610 // struct _objc_protocol {
2611 // struct _objc_protocol_extension *isa;
2612 // char *protocol_name;
2613 // struct _objc_protocol **_objc_protocol_list;
2614 // struct _objc_method_description_list *instance_methods;
2615 // struct _objc_method_description_list *class_methods;
2616 // }
2617 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002618 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002619 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2620 MethodDescriptionListPtrTy,
2621 MethodDescriptionListPtrTy,
2622 NULL);
2623 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2624
2625 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2626 CGM.getModule().addTypeName("struct._objc_protocol_list",
2627 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002628 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002629 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2630
2631 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002632 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002634
2635 // Class description structures
2636
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002637 // struct _objc_ivar {
2638 // char *ivar_name;
2639 // char *ivar_type;
2640 // int ivar_offset;
2641 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002642 IvarTy = llvm::StructType::get(Int8PtrTy,
2643 Int8PtrTy,
2644 IntTy,
2645 NULL);
2646 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2647
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002648 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002649 IvarListTy = llvm::OpaqueType::get();
2650 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2651 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2652
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002653 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002654 MethodListTy = llvm::OpaqueType::get();
2655 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2656 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2657
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002658 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002659 ClassExtensionTy =
2660 llvm::StructType::get(IntTy,
2661 Int8PtrTy,
2662 PropertyListPtrTy,
2663 NULL);
2664 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2665 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2666
2667 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2668
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002669 // struct _objc_class {
2670 // Class isa;
2671 // Class super_class;
2672 // char *name;
2673 // long version;
2674 // long info;
2675 // long instance_size;
2676 // struct _objc_ivar_list *ivars;
2677 // struct _objc_method_list *methods;
2678 // struct _objc_cache *cache;
2679 // struct _objc_protocol_list *protocols;
2680 // char *ivar_layout;
2681 // struct _objc_class_ext *ext;
2682 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002683 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2684 llvm::PointerType::getUnqual(ClassTyHolder),
2685 Int8PtrTy,
2686 LongTy,
2687 LongTy,
2688 LongTy,
2689 IvarListPtrTy,
2690 MethodListPtrTy,
2691 CachePtrTy,
2692 ProtocolListPtrTy,
2693 Int8PtrTy,
2694 ClassExtensionPtrTy,
2695 NULL);
2696 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2697
2698 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2699 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2700 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2701
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002702 // struct _objc_category {
2703 // char *category_name;
2704 // char *class_name;
2705 // struct _objc_method_list *instance_method;
2706 // struct _objc_method_list *class_method;
2707 // uint32_t size; // sizeof(struct _objc_category)
2708 // struct _objc_property_list *instance_properties;// category's @property
2709 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002710 CategoryTy = llvm::StructType::get(Int8PtrTy,
2711 Int8PtrTy,
2712 MethodListPtrTy,
2713 MethodListPtrTy,
2714 ProtocolListPtrTy,
2715 IntTy,
2716 PropertyListPtrTy,
2717 NULL);
2718 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2719
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002720 // Global metadata structures
2721
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002722 // struct _objc_symtab {
2723 // long sel_ref_cnt;
2724 // SEL *refs;
2725 // short cls_def_cnt;
2726 // short cat_def_cnt;
2727 // char *defs[cls_def_cnt + cat_def_cnt];
2728 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002729 SymtabTy = llvm::StructType::get(LongTy,
2730 SelectorPtrTy,
2731 ShortTy,
2732 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002733 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002734 NULL);
2735 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2736 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2737
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002738 // struct _objc_module {
2739 // long version;
2740 // long size; // sizeof(struct _objc_module)
2741 // char *name;
2742 // struct _objc_symtab* symtab;
2743 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002744 ModuleTy =
2745 llvm::StructType::get(LongTy,
2746 LongTy,
2747 Int8PtrTy,
2748 SymtabPtrTy,
2749 NULL);
2750 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002751
Daniel Dunbar49f66022008-09-24 03:38:44 +00002752 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002753
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002754 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002755 std::vector<const llvm::Type*> Params;
2756 Params.push_back(ObjectPtrTy);
2757 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002758 MessageSendFn =
2759 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2760 Params,
2761 true),
2762 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002763
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002764 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002765 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002766 Params.push_back(ObjectPtrTy);
2767 Params.push_back(SelectorPtrTy);
2768 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002769 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2770 Params,
2771 true),
2772 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002773
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002774 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002775 Params.clear();
2776 Params.push_back(ObjectPtrTy);
2777 Params.push_back(SelectorPtrTy);
2778 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002779 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002780 MessageSendFpretFn =
2781 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2782 Params,
2783 true),
2784 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002785
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002786 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002787 Params.clear();
2788 Params.push_back(SuperPtrTy);
2789 Params.push_back(SelectorPtrTy);
2790 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002791 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2792 Params,
2793 true),
2794 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002795
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002796 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2797 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002798 Params.clear();
2799 Params.push_back(Int8PtrTy);
2800 Params.push_back(SuperPtrTy);
2801 Params.push_back(SelectorPtrTy);
2802 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002803 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2804 Params,
2805 true),
2806 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002807
2808 // There is no objc_msgSendSuper_fpret? How can that work?
2809 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002810
Anders Carlsson124526b2008-09-09 10:10:21 +00002811 // FIXME: This is the size of the setjmp buffer and should be
2812 // target specific. 18 is what's used on 32-bit X86.
2813 uint64_t SetJmpBufferSize = 18;
2814
2815 // Exceptions
2816 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002817 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002818
2819 ExceptionDataTy =
2820 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2821 SetJmpBufferSize),
2822 StackPtrTy, NULL);
2823 CGM.getModule().addTypeName("struct._objc_exception_data",
2824 ExceptionDataTy);
2825
2826 Params.clear();
2827 Params.push_back(ObjectPtrTy);
2828 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002829 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2830 Params,
2831 false),
2832 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002833
2834 Params.clear();
2835 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2836 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002837 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2838 Params,
2839 false),
2840 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002841 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002842 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2843 Params,
2844 false),
2845 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002846 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002847 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2848 Params,
2849 false),
2850 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002851
2852 Params.clear();
2853 Params.push_back(ClassPtrTy);
2854 Params.push_back(ObjectPtrTy);
2855 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002856 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2857 Params,
2858 false),
2859 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002860
2861 // synchronized APIs
2862 // void objc_sync_enter (id)
2863 Params.clear();
2864 Params.push_back(ObjectPtrTy);
2865 SyncEnterFn =
2866 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2867 Params,
2868 false),
2869 "objc_sync_enter");
2870 // void objc_sync_exit (id)
2871 SyncExitFn =
2872 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2873 Params,
2874 false),
2875 "objc_sync_exit");
2876
Anders Carlsson124526b2008-09-09 10:10:21 +00002877
2878 Params.clear();
2879 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2880 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002881 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2882 Params,
2883 false),
2884 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002885
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002886}
2887
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002888ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002889: ObjCCommonTypesHelper(cgm)
2890{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002891 // struct _method_list_t {
2892 // uint32_t entsize; // sizeof(struct _objc_method)
2893 // uint32_t method_count;
2894 // struct _objc_method method_list[method_count];
2895 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002896 MethodListnfABITy = llvm::StructType::get(IntTy,
2897 IntTy,
2898 llvm::ArrayType::get(MethodTy, 0),
2899 NULL);
2900 CGM.getModule().addTypeName("struct.__method_list_t",
2901 MethodListnfABITy);
2902 // struct method_list_t *
2903 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002904
2905 // struct _protocol_t {
2906 // id isa; // NULL
2907 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002908 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002909 // const struct method_list_t * const instance_methods;
2910 // const struct method_list_t * const class_methods;
2911 // const struct method_list_t *optionalInstanceMethods;
2912 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002913 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002914 // const uint32_t size; // sizeof(struct _protocol_t)
2915 // const uint32_t flags; // = 0
2916 // }
2917
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002918 // Holder for struct _protocol_list_t *
2919 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2920
2921 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2922 Int8PtrTy,
2923 llvm::PointerType::getUnqual(
2924 ProtocolListTyHolder),
2925 MethodListnfABIPtrTy,
2926 MethodListnfABIPtrTy,
2927 MethodListnfABIPtrTy,
2928 MethodListnfABIPtrTy,
2929 PropertyListPtrTy,
2930 IntTy,
2931 IntTy,
2932 NULL);
2933 CGM.getModule().addTypeName("struct._protocol_t",
2934 ProtocolnfABITy);
2935
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002936 // struct _objc_protocol_list {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002937 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002938 // struct _protocol_t[protocol_count];
2939 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002940 ProtocolListnfABITy = llvm::StructType::get(LongTy,
2941 llvm::ArrayType::get(
2942 ProtocolnfABITy, 0),
2943 NULL);
2944 CGM.getModule().addTypeName("struct._objc_protocol_list",
2945 ProtocolListnfABITy);
2946
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002947 // struct _objc_protocol_list*
2948 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002949
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002950 // FIXME! Is this doing the right thing?
2951 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
2952 ProtocolListnfABIPtrTy);
2953
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002954 // struct _ivar_t {
2955 // unsigned long int *offset; // pointer to ivar offset location
2956 // char *name;
2957 // char *type;
2958 // uint32_t alignment;
2959 // uint32_t size;
2960 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002961 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
2962 Int8PtrTy,
2963 Int8PtrTy,
2964 IntTy,
2965 IntTy,
2966 NULL);
2967 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
2968
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002969 // struct _ivar_list_t {
2970 // uint32 entsize; // sizeof(struct _ivar_t)
2971 // uint32 count;
2972 // struct _iver_t list[count];
2973 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002974 IvarListnfABIPtrTy = llvm::StructType::get(IntTy,
2975 IntTy,
2976 llvm::ArrayType::get(
2977 IvarnfABITy, 0),
2978 NULL);
2979 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABIPtrTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002980
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002981 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002982 // uint32_t const flags;
2983 // uint32_t const instanceStart;
2984 // uint32_t const instanceSize;
2985 // uint32_t const reserved; // only when building for 64bit targets
2986 // const uint8_t * const ivarLayout;
2987 // const char *const name;
2988 // const struct _method_list_t * const baseMethods;
2989 // const struct _objc_protocol_list *const baseProtocols;
2990 // const struct _ivar_list_t *const ivars;
2991 // const uint8_t * const weakIvarLayout;
2992 // const struct _prop_list_t * const properties;
2993 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002994
2995 // FIXME. Add 'reserved' field in 64bit abi mode!
2996 ClassRonfABITy = llvm::StructType::get(IntTy,
2997 IntTy,
2998 IntTy,
2999 Int8PtrTy,
3000 Int8PtrTy,
3001 MethodListnfABIPtrTy,
3002 ProtocolListnfABIPtrTy,
3003 IvarListnfABIPtrTy,
3004 Int8PtrTy,
3005 PropertyListPtrTy,
3006 NULL);
3007 CGM.getModule().addTypeName("struct._class_ro_t",
3008 ClassRonfABITy);
3009
3010 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3011 std::vector<const llvm::Type*> Params;
3012 Params.push_back(ObjectPtrTy);
3013 Params.push_back(SelectorPtrTy);
3014 ImpnfABITy = llvm::PointerType::getUnqual(
3015 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3016
3017 // struct _class_t {
3018 // struct _class_t *isa;
3019 // struct _class_t * const superclass;
3020 // void *cache;
3021 // IMP *vtable;
3022 // struct class_ro_t *ro;
3023 // }
3024
3025 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3026 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3027 llvm::PointerType::getUnqual(ClassTyHolder),
3028 CachePtrTy,
3029 llvm::PointerType::getUnqual(ImpnfABITy),
3030 llvm::PointerType::getUnqual(
3031 ClassRonfABITy),
3032 NULL);
3033 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3034
3035 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3036 ClassnfABITy);
3037
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003038 // LLVM for struct _class_t *
3039 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3040
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003041 // struct _category_t {
3042 // const char * const name;
3043 // struct _class_t *const cls;
3044 // const struct _method_list_t * const instance_methods;
3045 // const struct _method_list_t * const class_methods;
3046 // const struct _protocol_list_t * const protocols;
3047 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003048 // }
3049 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003050 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003051 MethodListnfABIPtrTy,
3052 MethodListnfABIPtrTy,
3053 ProtocolListnfABIPtrTy,
3054 PropertyListPtrTy,
3055 NULL);
3056 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3057
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003058}
3059
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003060llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3061 FinishNonFragileABIModule();
3062
3063 return NULL;
3064}
3065
3066void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3067 // nonfragile abi has no module definition.
3068 std::vector<llvm::Constant*> Used;
3069 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3070 e = UsedGlobals.end(); i != e; ++i) {
3071 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3072 }
3073
3074 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3075 llvm::GlobalValue *GV =
3076 new llvm::GlobalVariable(AT, false,
3077 llvm::GlobalValue::AppendingLinkage,
3078 llvm::ConstantArray::get(AT, Used),
3079 "llvm.used",
3080 &CGM.getModule());
3081
3082 GV->setSection("llvm.metadata");
3083
3084}
3085
3086void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3087 std::string ClassName = ID->getNameAsString();
3088 if (!ObjCEmptyCacheVar) {
3089 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3090 ObjCTypes.CachePtrTy,
3091 false,
3092 llvm::GlobalValue::ExternalLinkage,
3093 0,
3094 "\01_objc_empty_cache",
3095 &CGM.getModule());
3096 UsedGlobals.push_back(ObjCEmptyCacheVar);
3097
3098 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3099 llvm::PointerType::getUnqual(
3100 ObjCTypes.ImpnfABITy),
3101 false,
3102 llvm::GlobalValue::ExternalLinkage,
3103 0,
3104 "\01_objc_empty_vtable",
3105 &CGM.getModule());
3106 UsedGlobals.push_back(ObjCEmptyVtableVar);
3107 }
3108
3109 std::vector<llvm::Constant*> Values(11); // 11 for 64bit targets!
3110 // Generate following meta-data:
3111 // struct _class_ro_t {
3112 // uint32_t const flags;
3113 // uint32_t const instanceStart;
3114 // uint32_t const instanceSize;
3115 // uint32_t const reserved; // only when building for 64bit targets
3116 // const uint8_t * const ivarLayout;
3117 // const char *const name;
3118 // const struct _method_list_t * const baseMethods;
3119 // const struct _objc_protocol_list *const baseProtocols;
3120 // const struct _ivar_list_t *const ivars;
3121 // const uint8_t * const weakIvarLayout;
3122 // const struct _prop_list_t * const properties;
3123 // }
3124 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3125 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3126 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3127 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3128 // FIXME. For 64bit targets add 0 here.
3129 Values[ 4] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3130 Values[ 5] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3131 Values[ 6] = llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3132 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3133 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3134 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3135 Values[10] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3136 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3137 Values);
3138 llvm::GlobalVariable *CLASS_RO_GV =
3139 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3140 llvm::GlobalValue::InternalLinkage,
3141 Init,
3142 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3143 &CGM.getModule());
3144 CLASS_RO_GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3145 UsedGlobals.push_back(CLASS_RO_GV);
3146
3147 // Generate following meta-data:
3148 // struct _class_t {
3149 // struct _class_t *isa;
3150 // struct _class_t * const superclass;
3151 // void *cache;
3152 // IMP *vtable;
3153 // struct class_ro_t *ro;
3154 // }
3155 Values.resize(5);
3156 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
3157 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
3158 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3159 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3160 Values[4] = CLASS_RO_GV; // &CLASS_RO_GV
3161 Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, Values);
3162 llvm::GlobalVariable *GV =
3163 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3164 llvm::GlobalValue::ExternalLinkage,
3165 Init,
3166 std::string("\01_OBJC_CLASS_$_")+ClassName,
3167 &CGM.getModule());
3168 GV->setSection(".section __DATA,__data,regular,no_dead_strip");
3169 UsedGlobals.push_back(GV);
3170 // FIXME! why?
3171 GV->setAlignment(32);
3172}
3173
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003174/* *** */
3175
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003176CodeGen::CGObjCRuntime *
3177CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003178 return new CGObjCMac(CGM);
3179}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003180
3181CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003182CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003183 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003184}