blob: 549a6b9f524137228b942ae1b7f3425740bad28e [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 Dunbar8ecbaf22009-02-24 07:47:38 +000023#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000024#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000025#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000027#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000028
29using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000030using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000031
32namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000033
Daniel Dunbarae226fa2008-08-27 02:31:56 +000034 typedef std::vector<llvm::Constant*> ConstantVector;
35
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000036 // FIXME: We should find a nicer way to make the labels for
37 // metadata, string concatenation is lame.
38
Fariborz Jahanianee0af742009-01-21 22:04:16 +000039class ObjCCommonTypesHelper {
40protected:
41 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000043public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000044 const llvm::Type *ShortTy, *IntTy, *LongTy;
45 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000046
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000047 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
48 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000049
50 /// PtrObjectPtrTy - LLVM type for id *
51 const llvm::Type *PtrObjectPtrTy;
52
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000053 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000054 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000055 /// ProtocolPtrTy - LLVM type for external protocol handles
56 /// (typeof(Protocol))
57 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000058
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000059 // SuperCTy - clang type for struct objc_super.
60 QualType SuperCTy;
61 // SuperPtrCTy - clang type for struct objc_super *.
62 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000063
Daniel Dunbare8b470d2008-08-23 04:28:29 +000064 /// SuperTy - LLVM type for struct objc_super.
65 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000066 /// SuperPtrTy - LLVM type for struct objc_super *.
67 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000068
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000069 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
70 /// in GCC parlance).
71 const llvm::StructType *PropertyTy;
72
73 /// PropertyListTy - LLVM type for struct objc_property_list
74 /// (_prop_list_t in GCC parlance).
75 const llvm::StructType *PropertyListTy;
76 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
77 const llvm::Type *PropertyListPtrTy;
78
79 // MethodTy - LLVM type for struct objc_method.
80 const llvm::StructType *MethodTy;
81
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000082 /// CacheTy - LLVM type for struct objc_cache.
83 const llvm::Type *CacheTy;
84 /// CachePtrTy - LLVM type for struct objc_cache *.
85 const llvm::Type *CachePtrTy;
86
Fariborz Jahaniandb286862009-01-22 00:37:21 +000087 llvm::Function *GetPropertyFn, *SetPropertyFn;
88
89 llvm::Function *EnumerationMutationFn;
90
91 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
92 llvm::Function *GcReadWeakFn;
93
94 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
95 llvm::Function *GcAssignWeakFn;
96
97 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
98 llvm::Function *GcAssignGlobalFn;
99
100 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
101 llvm::Function *GcAssignIvarFn;
102
103 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
104 llvm::Function *GcAssignStrongCastFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000105
106 /// ExceptionThrowFn - LLVM objc_exception_throw function.
107 llvm::Function *ExceptionThrowFn;
108
Daniel Dunbar1c566672009-02-24 01:43:46 +0000109 /// SyncEnterFn - LLVM object_sync_enter function.
110 llvm::Function *SyncEnterFn;
111
112 /// SyncExitFn - LLVM object_sync_exit function.
113 llvm::Function *SyncExitFn;
114
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000115 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
116 ~ObjCCommonTypesHelper(){}
117};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000118
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000119/// ObjCTypesHelper - Helper class that encapsulates lazy
120/// construction of varies types used during ObjC generation.
121class ObjCTypesHelper : public ObjCCommonTypesHelper {
122private:
123
124 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
125 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
126 *MessageSendSuperFpretFn;
127
128public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000129 /// SymtabTy - LLVM type for struct objc_symtab.
130 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000131 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
132 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000133 /// ModuleTy - LLVM type for struct objc_module.
134 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000135
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000136 /// ProtocolTy - LLVM type for struct objc_protocol.
137 const llvm::StructType *ProtocolTy;
138 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
139 const llvm::Type *ProtocolPtrTy;
140 /// ProtocolExtensionTy - LLVM type for struct
141 /// objc_protocol_extension.
142 const llvm::StructType *ProtocolExtensionTy;
143 /// ProtocolExtensionTy - LLVM type for struct
144 /// objc_protocol_extension *.
145 const llvm::Type *ProtocolExtensionPtrTy;
146 /// MethodDescriptionTy - LLVM type for struct
147 /// objc_method_description.
148 const llvm::StructType *MethodDescriptionTy;
149 /// MethodDescriptionListTy - LLVM type for struct
150 /// objc_method_description_list.
151 const llvm::StructType *MethodDescriptionListTy;
152 /// MethodDescriptionListPtrTy - LLVM type for struct
153 /// objc_method_description_list *.
154 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000155 /// ProtocolListTy - LLVM type for struct objc_property_list.
156 const llvm::Type *ProtocolListTy;
157 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
158 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000159 /// CategoryTy - LLVM type for struct objc_category.
160 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000161 /// ClassTy - LLVM type for struct objc_class.
162 const llvm::StructType *ClassTy;
163 /// ClassPtrTy - LLVM type for struct objc_class *.
164 const llvm::Type *ClassPtrTy;
165 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
166 const llvm::StructType *ClassExtensionTy;
167 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
168 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000169 // IvarTy - LLVM type for struct objc_ivar.
170 const llvm::StructType *IvarTy;
171 /// IvarListTy - LLVM type for struct objc_ivar_list.
172 const llvm::Type *IvarListTy;
173 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
174 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000175 /// MethodListTy - LLVM type for struct objc_method_list.
176 const llvm::Type *MethodListTy;
177 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
178 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000179
180 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
181 const llvm::Type *ExceptionDataTy;
182
Anders Carlsson124526b2008-09-09 10:10:21 +0000183 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
184 llvm::Function *ExceptionTryEnterFn;
185
186 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
187 llvm::Function *ExceptionTryExitFn;
188
189 /// ExceptionExtractFn - LLVM objc_exception_extract function.
190 llvm::Function *ExceptionExtractFn;
191
192 /// ExceptionMatchFn - LLVM objc_exception_match function.
193 llvm::Function *ExceptionMatchFn;
194
195 /// SetJmpFn - LLVM _setjmp function.
196 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000197
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000198public:
199 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000200 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000201
202
203 llvm::Function *getSendFn(bool IsSuper) {
204 return IsSuper ? MessageSendSuperFn : MessageSendFn;
205 }
206
207 llvm::Function *getSendStretFn(bool IsSuper) {
208 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
209 }
210
211 llvm::Function *getSendFpretFn(bool IsSuper) {
212 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
213 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000214};
215
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000216/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000217/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000218class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000219public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000220 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
221 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
222 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
223 *MessageSendSuper2StretFixupFn;
224
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000225 // MethodListnfABITy - LLVM for struct _method_list_t
226 const llvm::StructType *MethodListnfABITy;
227
228 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
229 const llvm::Type *MethodListnfABIPtrTy;
230
231 // ProtocolnfABITy = LLVM for struct _protocol_t
232 const llvm::StructType *ProtocolnfABITy;
233
Daniel Dunbar948e2582009-02-15 07:36:20 +0000234 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
235 const llvm::Type *ProtocolnfABIPtrTy;
236
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000237 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
238 const llvm::StructType *ProtocolListnfABITy;
239
240 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
241 const llvm::Type *ProtocolListnfABIPtrTy;
242
243 // ClassnfABITy - LLVM for struct _class_t
244 const llvm::StructType *ClassnfABITy;
245
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000246 // ClassnfABIPtrTy - LLVM for struct _class_t*
247 const llvm::Type *ClassnfABIPtrTy;
248
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000249 // IvarnfABITy - LLVM for struct _ivar_t
250 const llvm::StructType *IvarnfABITy;
251
252 // IvarListnfABITy - LLVM for struct _ivar_list_t
253 const llvm::StructType *IvarListnfABITy;
254
255 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
256 const llvm::Type *IvarListnfABIPtrTy;
257
258 // ClassRonfABITy - LLVM for struct _class_ro_t
259 const llvm::StructType *ClassRonfABITy;
260
261 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
262 const llvm::Type *ImpnfABITy;
263
264 // CategorynfABITy - LLVM for struct _category_t
265 const llvm::StructType *CategorynfABITy;
266
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000267 // New types for nonfragile abi messaging.
268
269 // MessageRefTy - LLVM for:
270 // struct _message_ref_t {
271 // IMP messenger;
272 // SEL name;
273 // };
274 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000275 // MessageRefCTy - clang type for struct _message_ref_t
276 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000277
278 // MessageRefPtrTy - LLVM for struct _message_ref_t*
279 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000280 // MessageRefCPtrTy - clang type for struct _message_ref_t*
281 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000282
Fariborz Jahanianef163782009-02-05 01:13:09 +0000283 // MessengerTy - Type of the messenger (shown as IMP above)
284 const llvm::FunctionType *MessengerTy;
285
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000286 // SuperMessageRefTy - LLVM for:
287 // struct _super_message_ref_t {
288 // SUPER_IMP messenger;
289 // SEL name;
290 // };
291 const llvm::StructType *SuperMessageRefTy;
292
293 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
294 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000295
296 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
297 /// exception personality function.
298 llvm::Value *EHPersonalityPtr;
299
300 llvm::Function *UnwindResumeOrRethrowFn;
301
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000302 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
303 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000304};
305
306class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
307protected:
308 CodeGen::CodeGenModule &CGM;
309 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000310 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000311
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000312 /// LazySymbols - Symbols to generate a lazy reference for. See
313 /// DefinedSymbols and FinishModule().
314 std::set<IdentifierInfo*> LazySymbols;
315
316 /// DefinedSymbols - External symbols which are defined by this
317 /// module. The symbols in this list and LazySymbols are used to add
318 /// special linker symbols which ensure that Objective-C modules are
319 /// linked properly.
320 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000322 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000323 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000324
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000325 /// MethodVarNames - uniqued method variable names.
326 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000327
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000328 /// MethodVarTypes - uniqued method type signatures. We have to use
329 /// a StringMap here because have no other unique reference.
330 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000331
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000332 /// MethodDefinitions - map of methods which have been defined in
333 /// this translation unit.
334 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000335
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000336 /// PropertyNames - uniqued method variable names.
337 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000338
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000339 /// ClassReferences - uniqued class references.
340 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000341
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000342 /// SelectorReferences - uniqued selector references.
343 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000344
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000345 /// Protocols - Protocols for which an objc_protocol structure has
346 /// been emitted. Forward declarations are handled by creating an
347 /// empty structure whose initializer is filled in when/if defined.
348 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000350 /// DefinedProtocols - Protocols which have actually been
351 /// defined. We should not need this, see FIXME in GenerateProtocol.
352 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000353
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000354 /// DefinedClasses - List of defined classes.
355 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000356
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000357 /// DefinedCategories - List of defined categories.
358 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000359
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000360 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000361 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000362 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000363
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000364 /// GetNameForMethod - Return a name for the given method.
365 /// \param[out] NameOut - The return value.
366 void GetNameForMethod(const ObjCMethodDecl *OMD,
367 const ObjCContainerDecl *CD,
368 std::string &NameOut);
369
370 /// GetMethodVarName - Return a unique constant for the given
371 /// selector's name. The return value has type char *.
372 llvm::Constant *GetMethodVarName(Selector Sel);
373 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
374 llvm::Constant *GetMethodVarName(const std::string &Name);
375
376 /// GetMethodVarType - Return a unique constant for the given
377 /// selector's name. The return value has type char *.
378
379 // FIXME: This is a horrible name.
380 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
381 llvm::Constant *GetMethodVarType(const std::string &Name);
382
383 /// GetPropertyName - Return a unique constant for the given
384 /// name. The return value has type char *.
385 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
386
387 // FIXME: This can be dropped once string functions are unified.
388 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
389 const Decl *Container);
390
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000391 /// GetClassName - Return a unique constant for the given selector's
392 /// name. The return value has type char *.
393 llvm::Constant *GetClassName(IdentifierInfo *Ident);
394
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000395 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
396 RecordDecl::field_iterator &FIV,
397 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000398 /// EmitPropertyList - Emit the given property list. The return
399 /// value has type PropertyListPtrTy.
400 llvm::Constant *EmitPropertyList(const std::string &Name,
401 const Decl *Container,
402 const ObjCContainerDecl *OCD,
403 const ObjCCommonTypesHelper &ObjCTypes);
404
Fariborz Jahanianda320092009-01-29 19:24:30 +0000405 /// GetProtocolRef - Return a reference to the internal protocol
406 /// description, creating an empty one if it has not been
407 /// defined. The return value has type ProtocolPtrTy.
408 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
409
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000410public:
411 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
412 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000413
414 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000415
416 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
417 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000418
419 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
420
421 /// GetOrEmitProtocol - Get the protocol object for the given
422 /// declaration, emitting it if necessary. The return value has type
423 /// ProtocolPtrTy.
424 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
425
426 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
427 /// object for the given declaration, emitting it if needed. These
428 /// forward references will be filled in with empty bodies if no
429 /// definition is seen. The return value has type ProtocolPtrTy.
430 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000431};
432
433class CGObjCMac : public CGObjCCommonMac {
434private:
435 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000436 /// EmitImageInfo - Emit the image info marker used to encode some module
437 /// level information.
438 void EmitImageInfo();
439
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000440 /// EmitModuleInfo - Another marker encoding module level
441 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000442 void EmitModuleInfo();
443
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000444 /// EmitModuleSymols - Emit module symbols, the list of defined
445 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000446 llvm::Constant *EmitModuleSymbols();
447
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000448 /// FinishModule - Write out global data structures at the end of
449 /// processing a translation unit.
450 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000451
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000452 /// EmitClassExtension - Generate the class extension structure used
453 /// to store the weak ivar layout and properties. The return value
454 /// has type ClassExtensionPtrTy.
455 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
456
457 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
458 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000459 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000460 const ObjCInterfaceDecl *ID);
461
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000462 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000463 QualType ResultType,
464 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000465 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000466 QualType Arg0Ty,
467 bool IsSuper,
468 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000469
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000470 /// EmitIvarList - Emit the ivar list for the given
471 /// implementation. If ForClass is true the list of class ivars
472 /// (i.e. metaclass ivars) is emitted, otherwise the list of
473 /// interface ivars will be emitted. The return value has type
474 /// IvarListPtrTy.
475 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000476 bool ForClass);
477
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000478 /// EmitMetaClass - Emit a forward reference to the class structure
479 /// for the metaclass of the given interface. The return value has
480 /// type ClassPtrTy.
481 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
482
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000484 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000485 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
486 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000487 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000488 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000489
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000490 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000491
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000492 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493
494 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000495 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000496 llvm::Constant *EmitMethodList(const std::string &Name,
497 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000498 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000499
500 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000501 /// method declarations.
502 /// - TypeName: The name for the type containing the methods.
503 /// - IsProtocol: True iff these methods are for a protocol.
504 /// - ClassMethds: True iff these are class methods.
505 /// - Required: When true, only "required" methods are
506 /// listed. Similarly, when false only "optional" methods are
507 /// listed. For classes this should always be true.
508 /// - begin, end: The method list to output.
509 ///
510 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000511 llvm::Constant *EmitMethodDescList(const std::string &Name,
512 const char *Section,
513 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000514
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000515 /// GetOrEmitProtocol - Get the protocol object for the given
516 /// declaration, emitting it if necessary. The return value has type
517 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000518 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000519
520 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
521 /// object for the given declaration, emitting it if needed. These
522 /// forward references will be filled in with empty bodies if no
523 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000524 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000525
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000526 /// EmitProtocolExtension - Generate the protocol extension
527 /// structure used to store optional instance and class methods, and
528 /// protocol properties. The return value has type
529 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000530 llvm::Constant *
531 EmitProtocolExtension(const ObjCProtocolDecl *PD,
532 const ConstantVector &OptInstanceMethods,
533 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000534
535 /// EmitProtocolList - Generate the list of referenced
536 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000537 llvm::Constant *EmitProtocolList(const std::string &Name,
538 ObjCProtocolDecl::protocol_iterator begin,
539 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000540
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
542 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000543 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000544
Fariborz Jahanianda320092009-01-29 19:24:30 +0000545 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000546 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000547
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000548 virtual llvm::Function *ModuleInitFunction();
549
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000550 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000551 QualType ResultType,
552 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000553 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000554 bool IsClassMessage,
555 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000556
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000557 virtual CodeGen::RValue
558 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000559 QualType ResultType,
560 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000561 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000562 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000563 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000564 bool IsClassMessage,
565 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000566
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000567 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000568 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000569
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000570 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000571
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000572 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000573
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000574 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000575
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000576 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000577 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000578
Daniel Dunbar49f66022008-09-24 03:38:44 +0000579 virtual llvm::Function *GetPropertyGetFunction();
580 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000581 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000582
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000583 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
584 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000585 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
586 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000587 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000588 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000589 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
590 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000591 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
592 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000593 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
594 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000595 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
596 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000597
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000598 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
599 QualType ObjectTy,
600 llvm::Value *BaseValue,
601 const ObjCIvarDecl *Ivar,
602 const FieldDecl *Field,
603 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000604 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
605 ObjCInterfaceDecl *Interface,
606 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000607};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000608
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000609class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000610private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000611 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000612 llvm::GlobalVariable* ObjCEmptyCacheVar;
613 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000614 /// MetaClassReferences - uniqued meta class references.
615 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000616
617 /// FinishNonFragileABIModule - Write out global data structures at the end of
618 /// processing a translation unit.
619 void FinishNonFragileABIModule();
620
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000621 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
622 unsigned InstanceStart,
623 unsigned InstanceSize,
624 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000625 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
626 llvm::Constant *IsAGV,
627 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000628 llvm::Constant *ClassRoGV,
629 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000630
631 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
632
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000633 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
634
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000635 /// EmitMethodList - Emit the method list for the given
636 /// implementation. The return value has type MethodListnfABITy.
637 llvm::Constant *EmitMethodList(const std::string &Name,
638 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000639 const ConstantVector &Methods);
640 /// EmitIvarList - Emit the ivar list for the given
641 /// implementation. If ForClass is true the list of class ivars
642 /// (i.e. metaclass ivars) is emitted, otherwise the list of
643 /// interface ivars will be emitted. The return value has type
644 /// IvarListnfABIPtrTy.
645 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000646
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000647 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000648 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000649 unsigned long int offset);
650
Fariborz Jahanianda320092009-01-29 19:24:30 +0000651 /// GetOrEmitProtocol - Get the protocol object for the given
652 /// declaration, emitting it if necessary. The return value has type
653 /// ProtocolPtrTy.
654 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
655
656 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
657 /// object for the given declaration, emitting it if needed. These
658 /// forward references will be filled in with empty bodies if no
659 /// definition is seen. The return value has type ProtocolPtrTy.
660 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
661
662 /// EmitProtocolList - Generate the list of referenced
663 /// protocols. The return value has type ProtocolListPtrTy.
664 llvm::Constant *EmitProtocolList(const std::string &Name,
665 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000666 ObjCProtocolDecl::protocol_iterator end);
667
668 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
669 QualType ResultType,
670 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000671 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000672 QualType Arg0Ty,
673 bool IsSuper,
674 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000675
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000676 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
677 /// for the given class.
678 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000679 const ObjCInterfaceDecl *ID,
680 bool IsSuper = false);
681
682 /// EmitMetaClassRef - Return a Value * of the address of _class_t
683 /// meta-data
684 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
685 const ObjCInterfaceDecl *ID);
686
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000687 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
688 /// the given ivar.
689 ///
690 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000691 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000692 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000693
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000694 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
695 /// for the given selector.
696 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
697
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000698public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000699 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000700 // FIXME. All stubs for now!
701 virtual llvm::Function *ModuleInitFunction();
702
703 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
704 QualType ResultType,
705 Selector Sel,
706 llvm::Value *Receiver,
707 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000708 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000709
710 virtual CodeGen::RValue
711 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
712 QualType ResultType,
713 Selector Sel,
714 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000715 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000716 llvm::Value *Receiver,
717 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000718 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000719
720 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000721 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000722
723 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000724 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000725
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000726 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000727
728 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000729 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000730 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000731
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000732 virtual llvm::Function *GetPropertyGetFunction(){
733 return ObjCTypes.GetPropertyFn;
734 }
735 virtual llvm::Function *GetPropertySetFunction(){
736 return ObjCTypes.SetPropertyFn;
737 }
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000738 virtual llvm::Function *EnumerationMutationFunction() {
739 return ObjCTypes.EnumerationMutationFn;
740 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000741
742 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000743 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000744 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000745 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000746 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000747 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000748 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000749 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000750 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000751 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000752 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000753 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000754 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000755 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000756 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
757 QualType ObjectTy,
758 llvm::Value *BaseValue,
759 const ObjCIvarDecl *Ivar,
760 const FieldDecl *Field,
761 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000762 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
763 ObjCInterfaceDecl *Interface,
764 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000765};
766
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000767} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000768
769/* *** Helper Functions *** */
770
771/// getConstantGEP() - Help routine to construct simple GEPs.
772static llvm::Constant *getConstantGEP(llvm::Constant *C,
773 unsigned idx0,
774 unsigned idx1) {
775 llvm::Value *Idxs[] = {
776 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
777 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
778 };
779 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
780}
781
782/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000783
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
785 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000786{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000787 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000788 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000789}
790
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000791/// GetClass - Return a reference to the class for the given interface
792/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000793llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000794 const ObjCInterfaceDecl *ID) {
795 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000796}
797
798/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000799llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000800 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000801}
802
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000803/// Generate a constant CFString object.
804/*
805 struct __builtin_CFString {
806 const int *isa; // point to __CFConstantStringClassReference
807 int flags;
808 const char *str;
809 long length;
810 };
811*/
812
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000813llvm::Constant *CGObjCCommonMac::GenerateConstantString(
814 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000815 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000816}
817
818/// Generates a message send where the super is the receiver. This is
819/// a message send to self with special delivery semantics indicating
820/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000821CodeGen::RValue
822CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000823 QualType ResultType,
824 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000825 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000826 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000827 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000828 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000829 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000830 // Create and init a super structure; this is a (receiver, class)
831 // pair we will pass to objc_msgSendSuper.
832 llvm::Value *ObjCSuper =
833 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
834 llvm::Value *ReceiverAsObject =
835 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
836 CGF.Builder.CreateStore(ReceiverAsObject,
837 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000838
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000839 // If this is a class message the metaclass is passed as the target.
840 llvm::Value *Target;
841 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000842 if (isCategoryImpl) {
843 // Message sent to 'super' in a class method defined in a category
844 // implementation requires an odd treatment.
845 // If we are in a class method, we must retrieve the
846 // _metaclass_ for the current class, pointed at by
847 // the class's "isa" pointer. The following assumes that
848 // isa" is the first ivar in a class (which it must be).
849 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
850 Target = CGF.Builder.CreateStructGEP(Target, 0);
851 Target = CGF.Builder.CreateLoad(Target);
852 }
853 else {
854 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
855 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
856 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
857 Target = Super;
858 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000859 } else {
860 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
861 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000862 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
863 // and ObjCTypes types.
864 const llvm::Type *ClassTy =
865 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000866 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000867 CGF.Builder.CreateStore(Target,
868 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
869
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000870 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000871 ObjCSuper, ObjCTypes.SuperPtrCTy,
872 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000873}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000874
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000875/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000876CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000877 QualType ResultType,
878 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000879 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000880 bool IsClassMessage,
881 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000882 llvm::Value *Arg0 =
883 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000884 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000885 Arg0, CGF.getContext().getObjCIdType(),
886 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000887}
888
889CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000890 QualType ResultType,
891 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000892 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000893 QualType Arg0Ty,
894 bool IsSuper,
895 const CallArgList &CallArgs) {
896 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000897 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
898 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
899 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000900 CGF.getContext().getObjCSelType()));
901 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000902
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000903 CodeGenTypes &Types = CGM.getTypes();
904 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
905 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000906
907 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000908 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000909 Fn = ObjCTypes.getSendStretFn(IsSuper);
910 } else if (ResultType->isFloatingType()) {
911 // FIXME: Sadly, this is wrong. This actually depends on the
912 // architecture. This happens to be right for x86-32 though.
913 Fn = ObjCTypes.getSendFpretFn(IsSuper);
914 } else {
915 Fn = ObjCTypes.getSendFn(IsSuper);
916 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000917 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000918 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000919}
920
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000921llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000922 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000923 // FIXME: I don't understand why gcc generates this, or where it is
924 // resolved. Investigate. Its also wasteful to look this up over and
925 // over.
926 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
927
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
929 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000930}
931
Fariborz Jahanianda320092009-01-29 19:24:30 +0000932void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000933 // FIXME: We shouldn't need this, the protocol decl should contain
934 // enough information to tell us whether this was a declaration or a
935 // definition.
936 DefinedProtocols.insert(PD->getIdentifier());
937
938 // If we have generated a forward reference to this protocol, emit
939 // it now. Otherwise do nothing, the protocol objects are lazily
940 // emitted.
941 if (Protocols.count(PD->getIdentifier()))
942 GetOrEmitProtocol(PD);
943}
944
Fariborz Jahanianda320092009-01-29 19:24:30 +0000945llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000946 if (DefinedProtocols.count(PD->getIdentifier()))
947 return GetOrEmitProtocol(PD);
948 return GetOrEmitProtocolRef(PD);
949}
950
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000951/*
952 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
953 struct _objc_protocol {
954 struct _objc_protocol_extension *isa;
955 char *protocol_name;
956 struct _objc_protocol_list *protocol_list;
957 struct _objc__method_prototype_list *instance_methods;
958 struct _objc__method_prototype_list *class_methods
959 };
960
961 See EmitProtocolExtension().
962*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000963llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
964 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
965
966 // Early exit if a defining object has already been generated.
967 if (Entry && Entry->hasInitializer())
968 return Entry;
969
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000970 // FIXME: I don't understand why gcc generates this, or where it is
971 // resolved. Investigate. Its also wasteful to look this up over and
972 // over.
973 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
974
Chris Lattner8ec03f52008-11-24 03:54:41 +0000975 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000976
977 // Construct method lists.
978 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
979 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
980 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
981 e = PD->instmeth_end(); i != e; ++i) {
982 ObjCMethodDecl *MD = *i;
983 llvm::Constant *C = GetMethodDescriptionConstant(MD);
984 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
985 OptInstanceMethods.push_back(C);
986 } else {
987 InstanceMethods.push_back(C);
988 }
989 }
990
991 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
992 e = PD->classmeth_end(); i != e; ++i) {
993 ObjCMethodDecl *MD = *i;
994 llvm::Constant *C = GetMethodDescriptionConstant(MD);
995 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
996 OptClassMethods.push_back(C);
997 } else {
998 ClassMethods.push_back(C);
999 }
1000 }
1001
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001003 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001004 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001005 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001006 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001007 PD->protocol_begin(),
1008 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001009 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001010 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1011 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001012 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1013 InstanceMethods);
1014 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001015 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1016 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1018 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1020 Values);
1021
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001023 // Already created, fix the linkage and update the initializer.
1024 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001025 Entry->setInitializer(Init);
1026 } else {
1027 Entry =
1028 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1029 llvm::GlobalValue::InternalLinkage,
1030 Init,
1031 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1032 &CGM.getModule());
1033 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1034 UsedGlobals.push_back(Entry);
1035 // FIXME: Is this necessary? Why only for protocol?
1036 Entry->setAlignment(4);
1037 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001038
1039 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040}
1041
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001042llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001043 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1044
1045 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001046 // We use the initializer as a marker of whether this is a forward
1047 // reference or not. At module finalization we add the empty
1048 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001049 Entry =
1050 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001051 llvm::GlobalValue::ExternalLinkage,
1052 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001053 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001054 &CGM.getModule());
1055 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1056 UsedGlobals.push_back(Entry);
1057 // FIXME: Is this necessary? Why only for protocol?
1058 Entry->setAlignment(4);
1059 }
1060
1061 return Entry;
1062}
1063
1064/*
1065 struct _objc_protocol_extension {
1066 uint32_t size;
1067 struct objc_method_description_list *optional_instance_methods;
1068 struct objc_method_description_list *optional_class_methods;
1069 struct objc_property_list *instance_properties;
1070 };
1071*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001072llvm::Constant *
1073CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1074 const ConstantVector &OptInstanceMethods,
1075 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001076 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001077 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001078 std::vector<llvm::Constant*> Values(4);
1079 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001080 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001081 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1082 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001083 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1084 OptInstanceMethods);
1085 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001086 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1087 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001088 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1089 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001090 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1091 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001092 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001093
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001094 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001095 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1096 Values[3]->isNullValue())
1097 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1098
1099 llvm::Constant *Init =
1100 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1101 llvm::GlobalVariable *GV =
1102 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1103 llvm::GlobalValue::InternalLinkage,
1104 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001105 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001106 &CGM.getModule());
1107 // No special section, but goes in llvm.used
1108 UsedGlobals.push_back(GV);
1109
1110 return GV;
1111}
1112
1113/*
1114 struct objc_protocol_list {
1115 struct objc_protocol_list *next;
1116 long count;
1117 Protocol *list[];
1118 };
1119*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001120llvm::Constant *
1121CGObjCMac::EmitProtocolList(const std::string &Name,
1122 ObjCProtocolDecl::protocol_iterator begin,
1123 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001124 std::vector<llvm::Constant*> ProtocolRefs;
1125
Daniel Dunbardbc933702008-08-21 21:57:41 +00001126 for (; begin != end; ++begin)
1127 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001128
1129 // Just return null for empty protocol lists
1130 if (ProtocolRefs.empty())
1131 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1132
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001133 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1135
1136 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001137 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1139 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1140 Values[2] =
1141 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1142 ProtocolRefs.size()),
1143 ProtocolRefs);
1144
1145 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1146 llvm::GlobalVariable *GV =
1147 new llvm::GlobalVariable(Init->getType(), false,
1148 llvm::GlobalValue::InternalLinkage,
1149 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001150 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001151 &CGM.getModule());
1152 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1153 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1154}
1155
1156/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001157 struct _objc_property {
1158 const char * const name;
1159 const char * const attributes;
1160 };
1161
1162 struct _objc_property_list {
1163 uint32_t entsize; // sizeof (struct _objc_property)
1164 uint32_t prop_count;
1165 struct _objc_property[prop_count];
1166 };
1167*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001168llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1169 const Decl *Container,
1170 const ObjCContainerDecl *OCD,
1171 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001172 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001173 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1174 E = OCD->prop_end(); I != E; ++I) {
1175 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001176 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001177 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001178 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1179 Prop));
1180 }
1181
1182 // Return null for empty list.
1183 if (Properties.empty())
1184 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1185
1186 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001187 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001188 std::vector<llvm::Constant*> Values(3);
1189 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1190 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1191 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1192 Properties.size());
1193 Values[2] = llvm::ConstantArray::get(AT, Properties);
1194 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1195
1196 llvm::GlobalVariable *GV =
1197 new llvm::GlobalVariable(Init->getType(), false,
1198 llvm::GlobalValue::InternalLinkage,
1199 Init,
1200 Name,
1201 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001202 if (ObjCABI == 2)
1203 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001204 // No special section on property lists?
1205 UsedGlobals.push_back(GV);
1206 return llvm::ConstantExpr::getBitCast(GV,
1207 ObjCTypes.PropertyListPtrTy);
1208
1209}
1210
1211/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001212 struct objc_method_description_list {
1213 int count;
1214 struct objc_method_description list[];
1215 };
1216*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001217llvm::Constant *
1218CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1219 std::vector<llvm::Constant*> Desc(2);
1220 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1221 ObjCTypes.SelectorPtrTy);
1222 Desc[1] = GetMethodVarType(MD);
1223 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1224 Desc);
1225}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001226
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001227llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1228 const char *Section,
1229 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001230 // Return null for empty list.
1231 if (Methods.empty())
1232 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1233
1234 std::vector<llvm::Constant*> Values(2);
1235 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1236 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1237 Methods.size());
1238 Values[1] = llvm::ConstantArray::get(AT, Methods);
1239 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1240
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001241 llvm::GlobalVariable *GV =
1242 new llvm::GlobalVariable(Init->getType(), false,
1243 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001244 Init, Name, &CGM.getModule());
1245 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001246 UsedGlobals.push_back(GV);
1247 return llvm::ConstantExpr::getBitCast(GV,
1248 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001249}
1250
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001251/*
1252 struct _objc_category {
1253 char *category_name;
1254 char *class_name;
1255 struct _objc_method_list *instance_methods;
1256 struct _objc_method_list *class_methods;
1257 struct _objc_protocol_list *protocols;
1258 uint32_t size; // <rdar://4585769>
1259 struct _objc_property_list *instance_properties;
1260 };
1261 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001262void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001263 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001264
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001265 // FIXME: This is poor design, the OCD should have a pointer to the
1266 // category decl. Additionally, note that Category can be null for
1267 // the @implementation w/o an @interface case. Sema should just
1268 // create one for us as it does for @implementation so everyone else
1269 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001270 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001271 const ObjCCategoryDecl *Category =
1272 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001273 std::string ExtName(Interface->getNameAsString() + "_" +
1274 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001275
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001276 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1277 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1278 e = OCD->instmeth_end(); i != e; ++i) {
1279 // Instance methods should always be defined.
1280 InstanceMethods.push_back(GetMethodConstant(*i));
1281 }
1282 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1283 e = OCD->classmeth_end(); i != e; ++i) {
1284 // Class methods should always be defined.
1285 ClassMethods.push_back(GetMethodConstant(*i));
1286 }
1287
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001288 std::vector<llvm::Constant*> Values(7);
1289 Values[0] = GetClassName(OCD->getIdentifier());
1290 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001291 Values[2] =
1292 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1293 ExtName,
1294 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001295 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001296 Values[3] =
1297 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1298 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001299 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001300 if (Category) {
1301 Values[4] =
1302 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1303 Category->protocol_begin(),
1304 Category->protocol_end());
1305 } else {
1306 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1307 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001308 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001309
1310 // If there is no category @interface then there can be no properties.
1311 if (Category) {
1312 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001313 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001314 } else {
1315 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1316 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001317
1318 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1319 Values);
1320
1321 llvm::GlobalVariable *GV =
1322 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1323 llvm::GlobalValue::InternalLinkage,
1324 Init,
1325 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1326 &CGM.getModule());
1327 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1328 UsedGlobals.push_back(GV);
1329 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001330}
1331
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001332// FIXME: Get from somewhere?
1333enum ClassFlags {
1334 eClassFlags_Factory = 0x00001,
1335 eClassFlags_Meta = 0x00002,
1336 // <rdr://5142207>
1337 eClassFlags_HasCXXStructors = 0x02000,
1338 eClassFlags_Hidden = 0x20000,
1339 eClassFlags_ABI2_Hidden = 0x00010,
1340 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1341};
1342
1343// <rdr://5142207&4705298&4843145>
1344static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1345 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1346 // FIXME: Support -fvisibility
1347 switch (attr->getVisibility()) {
1348 default:
1349 assert(0 && "Unknown visibility");
1350 return false;
1351 case VisibilityAttr::DefaultVisibility:
1352 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1353 return false;
1354 case VisibilityAttr::HiddenVisibility:
1355 return true;
1356 }
1357 } else {
1358 return false; // FIXME: Support -fvisibility
1359 }
1360}
1361
1362/*
1363 struct _objc_class {
1364 Class isa;
1365 Class super_class;
1366 const char *name;
1367 long version;
1368 long info;
1369 long instance_size;
1370 struct _objc_ivar_list *ivars;
1371 struct _objc_method_list *methods;
1372 struct _objc_cache *cache;
1373 struct _objc_protocol_list *protocols;
1374 // Objective-C 1.0 extensions (<rdr://4585769>)
1375 const char *ivar_layout;
1376 struct _objc_class_ext *ext;
1377 };
1378
1379 See EmitClassExtension();
1380 */
1381void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001382 DefinedSymbols.insert(ID->getIdentifier());
1383
Chris Lattner8ec03f52008-11-24 03:54:41 +00001384 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001385 // FIXME: Gross
1386 ObjCInterfaceDecl *Interface =
1387 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001388 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001389 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001390 Interface->protocol_begin(),
1391 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001392 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001393 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001394 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001395 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001396
1397 // FIXME: Set CXX-structors flag.
1398 if (IsClassHidden(ID->getClassInterface()))
1399 Flags |= eClassFlags_Hidden;
1400
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001401 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1402 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1403 e = ID->instmeth_end(); i != e; ++i) {
1404 // Instance methods should always be defined.
1405 InstanceMethods.push_back(GetMethodConstant(*i));
1406 }
1407 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1408 e = ID->classmeth_end(); i != e; ++i) {
1409 // Class methods should always be defined.
1410 ClassMethods.push_back(GetMethodConstant(*i));
1411 }
1412
1413 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1414 e = ID->propimpl_end(); i != e; ++i) {
1415 ObjCPropertyImplDecl *PID = *i;
1416
1417 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1418 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1419
1420 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1421 if (llvm::Constant *C = GetMethodConstant(MD))
1422 InstanceMethods.push_back(C);
1423 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1424 if (llvm::Constant *C = GetMethodConstant(MD))
1425 InstanceMethods.push_back(C);
1426 }
1427 }
1428
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001429 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001430 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001432 // Record a reference to the super class.
1433 LazySymbols.insert(Super->getIdentifier());
1434
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 Values[ 1] =
1436 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1437 ObjCTypes.ClassPtrTy);
1438 } else {
1439 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1440 }
1441 Values[ 2] = GetClassName(ID->getIdentifier());
1442 // Version is always 0.
1443 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1444 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1445 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001446 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001447 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001448 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001449 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001450 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001451 // cache is always NULL.
1452 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1453 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001454 // FIXME: Set ivar_layout
1455 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001456 Values[11] = EmitClassExtension(ID);
1457 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1458 Values);
1459
1460 llvm::GlobalVariable *GV =
1461 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1462 llvm::GlobalValue::InternalLinkage,
1463 Init,
1464 std::string("\01L_OBJC_CLASS_")+ClassName,
1465 &CGM.getModule());
1466 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1467 UsedGlobals.push_back(GV);
1468 // FIXME: Why?
1469 GV->setAlignment(32);
1470 DefinedClasses.push_back(GV);
1471}
1472
1473llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1474 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001475 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001476 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001477 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001478 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001479
1480 if (IsClassHidden(ID->getClassInterface()))
1481 Flags |= eClassFlags_Hidden;
1482
1483 std::vector<llvm::Constant*> Values(12);
1484 // The isa for the metaclass is the root of the hierarchy.
1485 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1486 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1487 Root = Super;
1488 Values[ 0] =
1489 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1490 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001491 // The super class for the metaclass is emitted as the name of the
1492 // super class. The runtime fixes this up to point to the
1493 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001494 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1495 Values[ 1] =
1496 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1497 ObjCTypes.ClassPtrTy);
1498 } else {
1499 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1500 }
1501 Values[ 2] = GetClassName(ID->getIdentifier());
1502 // Version is always 0.
1503 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1504 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1505 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001506 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001507 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001508 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001509 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001510 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001511 // cache is always NULL.
1512 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1513 Values[ 9] = Protocols;
1514 // ivar_layout for metaclass is always NULL.
1515 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1516 // The class extension is always unused for metaclasses.
1517 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1518 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1519 Values);
1520
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001521 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001522 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001523
1524 // Check for a forward reference.
1525 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1526 if (GV) {
1527 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1528 "Forward metaclass reference has incorrect type.");
1529 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1530 GV->setInitializer(Init);
1531 } else {
1532 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1533 llvm::GlobalValue::InternalLinkage,
1534 Init, Name,
1535 &CGM.getModule());
1536 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001537 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1538 UsedGlobals.push_back(GV);
1539 // FIXME: Why?
1540 GV->setAlignment(32);
1541
1542 return GV;
1543}
1544
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001545llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001546 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001547
1548 // FIXME: Should we look these up somewhere other than the
1549 // module. Its a bit silly since we only generate these while
1550 // processing an implementation, so exactly one pointer would work
1551 // if know when we entered/exitted an implementation block.
1552
1553 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001554 // Previously, metaclass with internal linkage may have been defined.
1555 // pass 'true' as 2nd argument so it is returned.
1556 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001557 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1558 "Forward metaclass reference has incorrect type.");
1559 return GV;
1560 } else {
1561 // Generate as an external reference to keep a consistent
1562 // module. This will be patched up when we emit the metaclass.
1563 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1564 llvm::GlobalValue::ExternalLinkage,
1565 0,
1566 Name,
1567 &CGM.getModule());
1568 }
1569}
1570
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001571/*
1572 struct objc_class_ext {
1573 uint32_t size;
1574 const char *weak_ivar_layout;
1575 struct _objc_property_list *properties;
1576 };
1577*/
1578llvm::Constant *
1579CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1580 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001581 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001582
1583 std::vector<llvm::Constant*> Values(3);
1584 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001585 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001586 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001587 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001588 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001589
1590 // Return null if no extension bits are used.
1591 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1592 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1593
1594 llvm::Constant *Init =
1595 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1596 llvm::GlobalVariable *GV =
1597 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1598 llvm::GlobalValue::InternalLinkage,
1599 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001600 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001601 &CGM.getModule());
1602 // No special section, but goes in llvm.used
1603 UsedGlobals.push_back(GV);
1604
1605 return GV;
1606}
1607
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001608/// countInheritedIvars - count number of ivars in class and its super class(s)
1609///
1610static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1611 int count = 0;
1612 if (!OI)
1613 return 0;
1614 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1615 if (SuperClass)
1616 count += countInheritedIvars(SuperClass);
1617 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1618 E = OI->ivar_end(); I != E; ++I)
1619 ++count;
1620 return count;
1621}
1622
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001623/// getInterfaceDeclForIvar - Get the interface declaration node where
1624/// this ivar is declared in.
1625/// FIXME. Ideally, this info should be in the ivar node. But currently
1626/// it is not and prevailing wisdom is that ASTs should not have more
1627/// info than is absolutely needed, even though this info reflects the
1628/// source language.
1629///
1630static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1631 const ObjCInterfaceDecl *OI,
1632 const ObjCIvarDecl *IVD) {
1633 if (!OI)
1634 return 0;
1635 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1636 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1637 E = OI->ivar_end(); I != E; ++I)
1638 if ((*I)->getIdentifier() == IVD->getIdentifier())
1639 return OI;
1640 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1641}
1642
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001643/*
1644 struct objc_ivar {
1645 char *ivar_name;
1646 char *ivar_type;
1647 int ivar_offset;
1648 };
1649
1650 struct objc_ivar_list {
1651 int ivar_count;
1652 struct objc_ivar list[count];
1653 };
1654 */
1655llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001656 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001657 std::vector<llvm::Constant*> Ivars, Ivar(3);
1658
1659 // When emitting the root class GCC emits ivar entries for the
1660 // actual class structure. It is not clear if we need to follow this
1661 // behavior; for now lets try and get away with not doing it. If so,
1662 // the cleanest solution would be to make up an ObjCInterfaceDecl
1663 // for the class.
1664 if (ForClass)
1665 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001666
1667 ObjCInterfaceDecl *OID =
1668 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1669 const llvm::Type *InterfaceTy =
1670 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001671 const llvm::StructLayout *Layout =
1672 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001673
1674 RecordDecl::field_iterator ifield, pfield;
1675 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001676 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1677 FieldDecl *Field = *ifield;
1678 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1679 getLLVMFieldNo(Field));
1680 if (Field->getIdentifier())
1681 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1682 else
1683 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001684 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001685 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001686 Ivar[1] = GetMethodVarType(TypeStr);
1687 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001688 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001689 }
1690
1691 // Return null for empty list.
1692 if (Ivars.empty())
1693 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1694
1695 std::vector<llvm::Constant*> Values(2);
1696 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1697 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1698 Ivars.size());
1699 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1700 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1701
1702 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1703 "\01L_OBJC_INSTANCE_VARIABLES_");
1704 llvm::GlobalVariable *GV =
1705 new llvm::GlobalVariable(Init->getType(), false,
1706 llvm::GlobalValue::InternalLinkage,
1707 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001708 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001709 &CGM.getModule());
1710 if (ForClass) {
1711 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1712 // FIXME: Why is this only here?
1713 GV->setAlignment(32);
1714 } else {
1715 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1716 }
1717 UsedGlobals.push_back(GV);
1718 return llvm::ConstantExpr::getBitCast(GV,
1719 ObjCTypes.IvarListPtrTy);
1720}
1721
1722/*
1723 struct objc_method {
1724 SEL method_name;
1725 char *method_types;
1726 void *method;
1727 };
1728
1729 struct objc_method_list {
1730 struct objc_method_list *obsolete;
1731 int count;
1732 struct objc_method methods_list[count];
1733 };
1734*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001735
1736/// GetMethodConstant - Return a struct objc_method constant for the
1737/// given method if it has been defined. The result is null if the
1738/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001739llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001740 // FIXME: Use DenseMap::lookup
1741 llvm::Function *Fn = MethodDefinitions[MD];
1742 if (!Fn)
1743 return 0;
1744
1745 std::vector<llvm::Constant*> Method(3);
1746 Method[0] =
1747 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1748 ObjCTypes.SelectorPtrTy);
1749 Method[1] = GetMethodVarType(MD);
1750 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1751 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1752}
1753
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001754llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1755 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001756 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001757 // Return null for empty list.
1758 if (Methods.empty())
1759 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1760
1761 std::vector<llvm::Constant*> Values(3);
1762 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1763 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1764 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1765 Methods.size());
1766 Values[2] = llvm::ConstantArray::get(AT, Methods);
1767 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1768
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001769 llvm::GlobalVariable *GV =
1770 new llvm::GlobalVariable(Init->getType(), false,
1771 llvm::GlobalValue::InternalLinkage,
1772 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001773 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001774 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001775 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001776 UsedGlobals.push_back(GV);
1777 return llvm::ConstantExpr::getBitCast(GV,
1778 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001779}
1780
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001781llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001782 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001783 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001784 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001785
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001786 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001787 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001788 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001789 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001790 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001791 llvm::GlobalValue::InternalLinkage,
1792 Name,
1793 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001794 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001795
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001796 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001797}
1798
1799llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001800 // Abuse this interface function as a place to finalize.
1801 FinishModule();
1802
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001803 return NULL;
1804}
1805
Daniel Dunbar49f66022008-09-24 03:38:44 +00001806llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1807 return ObjCTypes.GetPropertyFn;
1808}
1809
1810llvm::Function *CGObjCMac::GetPropertySetFunction() {
1811 return ObjCTypes.SetPropertyFn;
1812}
1813
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001814llvm::Function *CGObjCMac::EnumerationMutationFunction()
1815{
1816 return ObjCTypes.EnumerationMutationFn;
1817}
1818
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001819/*
1820
1821Objective-C setjmp-longjmp (sjlj) Exception Handling
1822--
1823
1824The basic framework for a @try-catch-finally is as follows:
1825{
1826 objc_exception_data d;
1827 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001828 bool _call_try_exit = true;
1829
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001830 objc_exception_try_enter(&d);
1831 if (!setjmp(d.jmp_buf)) {
1832 ... try body ...
1833 } else {
1834 // exception path
1835 id _caught = objc_exception_extract(&d);
1836
1837 // enter new try scope for handlers
1838 if (!setjmp(d.jmp_buf)) {
1839 ... match exception and execute catch blocks ...
1840
1841 // fell off end, rethrow.
1842 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001843 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001844 } else {
1845 // exception in catch block
1846 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001847 _call_try_exit = false;
1848 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001849 }
1850 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001851 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001852
1853finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001854 if (_call_try_exit)
1855 objc_exception_try_exit(&d);
1856
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001857 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001858 ... dispatch to finally destination ...
1859
1860finally_rethrow:
1861 objc_exception_throw(_rethrow);
1862
1863finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001864}
1865
1866This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001867uses _rethrow to determine if objc_exception_try_exit should be called
1868and if the object should be rethrown. This breaks in the face of
1869throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001870
1871We specialize this framework for a few particular circumstances:
1872
1873 - If there are no catch blocks, then we avoid emitting the second
1874 exception handling context.
1875
1876 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1877 e)) we avoid emitting the code to rethrow an uncaught exception.
1878
1879 - FIXME: If there is no @finally block we can do a few more
1880 simplifications.
1881
1882Rethrows and Jumps-Through-Finally
1883--
1884
1885Support for implicit rethrows and jumping through the finally block is
1886handled by storing the current exception-handling context in
1887ObjCEHStack.
1888
Daniel Dunbar898d5082008-09-30 01:06:03 +00001889In order to implement proper @finally semantics, we support one basic
1890mechanism for jumping through the finally block to an arbitrary
1891destination. Constructs which generate exits from a @try or @catch
1892block use this mechanism to implement the proper semantics by chaining
1893jumps, as necessary.
1894
1895This mechanism works like the one used for indirect goto: we
1896arbitrarily assign an ID to each destination and store the ID for the
1897destination in a variable prior to entering the finally block. At the
1898end of the finally block we simply create a switch to the proper
1899destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001900
1901Code gen for @synchronized(expr) stmt;
1902Effectively generating code for:
1903objc_sync_enter(expr);
1904@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001905*/
1906
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001907void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1908 const Stmt &S) {
1909 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001910 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001911 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001912 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001913 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1914 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1915 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00001916
1917 // For @synchronized, call objc_sync_enter(sync.expr). The
1918 // evaluation of the expression must occur before we enter the
1919 // @synchronized. We can safely avoid a temp here because jumps into
1920 // @synchronized are illegal & this will dominate uses.
1921 llvm::Value *SyncArg = 0;
1922 if (!isTry) {
1923 SyncArg =
1924 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1925 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
1926 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
1927 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001928
1929 // Push an EH context entry, used for handling rethrows and jumps
1930 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001931 CGF.PushCleanupBlock(FinallyBlock);
1932
Anders Carlsson273558f2009-02-07 21:37:21 +00001933 CGF.ObjCEHValueStack.push_back(0);
1934
Daniel Dunbar898d5082008-09-30 01:06:03 +00001935 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001936 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1937 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001938 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1939 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001940 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1941 "_call_try_exit");
1942 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1943
Anders Carlsson80f25672008-09-09 17:59:25 +00001944 // Enter a new try block and call setjmp.
1945 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1946 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1947 "jmpbufarray");
1948 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1949 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1950 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001951
Daniel Dunbar55e87422008-11-11 02:29:29 +00001952 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1953 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001954 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001955 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001956
1957 // Emit the @try block.
1958 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001959 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1960 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001961 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001962
1963 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001964 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001965
1966 // Retrieve the exception object. We may emit multiple blocks but
1967 // nothing can cross this so the value is already in SSA form.
1968 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1969 ExceptionData,
1970 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001971 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001972 if (!isTry)
1973 {
1974 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001975 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001976 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001977 }
1978 else if (const ObjCAtCatchStmt* CatchStmt =
1979 cast<ObjCAtTryStmt>(S).getCatchStmts())
1980 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001981 // Enter a new exception try block (in case a @catch block throws
1982 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001983 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001984
Anders Carlsson80f25672008-09-09 17:59:25 +00001985 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1986 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001987 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001988
Daniel Dunbar55e87422008-11-11 02:29:29 +00001989 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1990 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001991 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001992
1993 CGF.EmitBlock(CatchBlock);
1994
Daniel Dunbar55e40722008-09-27 07:03:52 +00001995 // Handle catch list. As a special case we check if everything is
1996 // matched and avoid generating code for falling off the end if
1997 // so.
1998 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001999 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002000 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002001
Anders Carlssondde0a942008-09-11 09:15:33 +00002002 const DeclStmt *CatchParam =
2003 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00002004 const VarDecl *VD = 0;
2005 const PointerType *PT = 0;
2006
Anders Carlsson80f25672008-09-09 17:59:25 +00002007 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002008 if (!CatchParam) {
2009 AllMatched = true;
2010 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00002011 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00002012 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002013
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002014 // catch(id e) always matches.
2015 // FIXME: For the time being we also match id<X>; this should
2016 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002017 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002018 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002019 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002020 }
2021
Daniel Dunbar55e40722008-09-27 07:03:52 +00002022 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002023 if (CatchParam) {
2024 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002025 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002026 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002027 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002028
Anders Carlssondde0a942008-09-11 09:15:33 +00002029 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002030 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002031 break;
2032 }
2033
Daniel Dunbar129271a2008-09-27 07:36:24 +00002034 assert(PT && "Unexpected non-pointer type in @catch");
2035 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002036 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002037 assert(ObjCType && "Catch parameter must have Objective-C type!");
2038
2039 // Check if the @catch block matches the exception object.
2040 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2041
Anders Carlsson80f25672008-09-09 17:59:25 +00002042 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2043 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002044
Daniel Dunbar55e87422008-11-11 02:29:29 +00002045 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002046
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002047 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002048 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002049
2050 // Emit the @catch block.
2051 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002052 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002053 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002054
2055 llvm::Value *Tmp =
2056 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2057 "tmp");
2058 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002059
2060 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002061 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002062
2063 CGF.EmitBlock(NextCatchBlock);
2064 }
2065
Daniel Dunbar55e40722008-09-27 07:03:52 +00002066 if (!AllMatched) {
2067 // None of the handlers caught the exception, so store it to be
2068 // rethrown at the end of the @finally block.
2069 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002070 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002071 }
2072
2073 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002074 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002075 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2076 ExceptionData),
2077 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002078 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002079 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002080 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002081 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002082 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002083 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002084 }
2085
Daniel Dunbar898d5082008-09-30 01:06:03 +00002086 // Pop the exception-handling stack entry. It is important to do
2087 // this now, because the code in the @finally block is not in this
2088 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002089 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2090
Anders Carlsson273558f2009-02-07 21:37:21 +00002091 CGF.ObjCEHValueStack.pop_back();
2092
Anders Carlsson80f25672008-09-09 17:59:25 +00002093 // Emit the @finally block.
2094 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002095 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2096
2097 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2098
2099 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002100 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002101
2102 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002103 if (isTry) {
2104 if (const ObjCAtFinallyStmt* FinallyStmt =
2105 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2106 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002107 } else {
2108 // Emit objc_sync_exit(expr); as finally's sole statement for
2109 // @synchronized.
2110 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002111 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002112
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002113 // Emit the switch block
2114 if (Info.SwitchBlock)
2115 CGF.EmitBlock(Info.SwitchBlock);
2116 if (Info.EndBlock)
2117 CGF.EmitBlock(Info.EndBlock);
2118
Daniel Dunbar898d5082008-09-30 01:06:03 +00002119 CGF.EmitBlock(FinallyRethrow);
2120 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2121 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002122 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002123
2124 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002125}
2126
2127void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002128 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002129 llvm::Value *ExceptionAsObject;
2130
2131 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2132 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2133 ExceptionAsObject =
2134 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2135 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002136 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002137 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002138 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002139 }
2140
2141 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002142 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002143
2144 // Clear the insertion point to indicate we are in unreachable code.
2145 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002146}
2147
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002148/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002149/// object: objc_read_weak (id *src)
2150///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002151llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002152 llvm::Value *AddrWeakObj)
2153{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002154 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002155 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002156 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002157 return read_weak;
2158}
2159
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002160/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2161/// objc_assign_weak (id src, id *dst)
2162///
2163void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2164 llvm::Value *src, llvm::Value *dst)
2165{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002166 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2167 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002168 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2169 src, dst, "weakassign");
2170 return;
2171}
2172
Fariborz Jahanian58626502008-11-19 00:59:10 +00002173/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2174/// objc_assign_global (id src, id *dst)
2175///
2176void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2177 llvm::Value *src, llvm::Value *dst)
2178{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002179 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2180 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002181 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2182 src, dst, "globalassign");
2183 return;
2184}
2185
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002186/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2187/// objc_assign_ivar (id src, id *dst)
2188///
2189void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2190 llvm::Value *src, llvm::Value *dst)
2191{
2192 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2193 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2194 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2195 src, dst, "assignivar");
2196 return;
2197}
2198
Fariborz Jahanian58626502008-11-19 00:59:10 +00002199/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2200/// objc_assign_strongCast (id src, id *dst)
2201///
2202void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2203 llvm::Value *src, llvm::Value *dst)
2204{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002205 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2206 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002207 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2208 src, dst, "weakassign");
2209 return;
2210}
2211
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002212/// EmitObjCValueForIvar - Code Gen for ivar reference.
2213///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002214LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2215 QualType ObjectTy,
2216 llvm::Value *BaseValue,
2217 const ObjCIvarDecl *Ivar,
2218 const FieldDecl *Field,
2219 unsigned CVRQualifiers) {
2220 if (Ivar->isBitField())
2221 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2222 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002223 // TODO: Add a special case for isa (index 0)
2224 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2225 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002226 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002227 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2228 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002229 LValue::SetObjCIvar(LV, true);
2230 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002231}
2232
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002233llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2234 ObjCInterfaceDecl *Interface,
2235 const ObjCIvarDecl *Ivar) {
2236 const llvm::Type *InterfaceLTy =
2237 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2238 const llvm::StructLayout *Layout =
2239 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2240 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2241 uint64_t Offset =
2242 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2243
2244 return llvm::ConstantInt::get(
2245 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2246 Offset);
2247}
2248
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002249/* *** Private Interface *** */
2250
2251/// EmitImageInfo - Emit the image info marker used to encode some module
2252/// level information.
2253///
2254/// See: <rdr://4810609&4810587&4810587>
2255/// struct IMAGE_INFO {
2256/// unsigned version;
2257/// unsigned flags;
2258/// };
2259enum ImageInfoFlags {
2260 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2261 eImageInfo_GarbageCollected = (1 << 1),
2262 eImageInfo_GCOnly = (1 << 2)
2263};
2264
2265void CGObjCMac::EmitImageInfo() {
2266 unsigned version = 0; // Version is unused?
2267 unsigned flags = 0;
2268
2269 // FIXME: Fix and continue?
2270 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2271 flags |= eImageInfo_GarbageCollected;
2272 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2273 flags |= eImageInfo_GCOnly;
2274
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002275 // Emitted as int[2];
2276 llvm::Constant *values[2] = {
2277 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2278 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2279 };
2280 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002281 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002282 new llvm::GlobalVariable(AT, true,
2283 llvm::GlobalValue::InternalLinkage,
2284 llvm::ConstantArray::get(AT, values, 2),
2285 "\01L_OBJC_IMAGE_INFO",
2286 &CGM.getModule());
2287
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002288 if (ObjCABI == 1) {
2289 GV->setSection("__OBJC, __image_info,regular");
2290 } else {
2291 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2292 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002293
2294 UsedGlobals.push_back(GV);
2295}
2296
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002297
2298// struct objc_module {
2299// unsigned long version;
2300// unsigned long size;
2301// const char *name;
2302// Symtab symtab;
2303// };
2304
2305// FIXME: Get from somewhere
2306static const int ModuleVersion = 7;
2307
2308void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002309 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002310
2311 std::vector<llvm::Constant*> Values(4);
2312 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2313 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002314 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002315 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002316 Values[3] = EmitModuleSymbols();
2317
2318 llvm::GlobalVariable *GV =
2319 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2320 llvm::GlobalValue::InternalLinkage,
2321 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2322 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002323 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002324 &CGM.getModule());
2325 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2326 UsedGlobals.push_back(GV);
2327}
2328
2329llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002330 unsigned NumClasses = DefinedClasses.size();
2331 unsigned NumCategories = DefinedCategories.size();
2332
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002333 // Return null if no symbols were defined.
2334 if (!NumClasses && !NumCategories)
2335 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2336
2337 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002338 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2339 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2340 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2341 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2342
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002343 // The runtime expects exactly the list of defined classes followed
2344 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002346 for (unsigned i=0; i<NumClasses; i++)
2347 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2348 ObjCTypes.Int8PtrTy);
2349 for (unsigned i=0; i<NumCategories; i++)
2350 Symbols[NumClasses + i] =
2351 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2352 ObjCTypes.Int8PtrTy);
2353
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002355 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002356 NumClasses + NumCategories),
2357 Symbols);
2358
2359 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2360
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002361 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002362 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002363 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002364 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002365 "\01L_OBJC_SYMBOLS",
2366 &CGM.getModule());
2367 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2368 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002369 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2370}
2371
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002372llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002374 LazySymbols.insert(ID->getIdentifier());
2375
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002376 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2377
2378 if (!Entry) {
2379 llvm::Constant *Casted =
2380 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2381 ObjCTypes.ClassPtrTy);
2382 Entry =
2383 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2384 llvm::GlobalValue::InternalLinkage,
2385 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2386 &CGM.getModule());
2387 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2388 UsedGlobals.push_back(Entry);
2389 }
2390
2391 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002392}
2393
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002394llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002395 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2396
2397 if (!Entry) {
2398 llvm::Constant *Casted =
2399 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2400 ObjCTypes.SelectorPtrTy);
2401 Entry =
2402 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2403 llvm::GlobalValue::InternalLinkage,
2404 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2405 &CGM.getModule());
2406 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2407 UsedGlobals.push_back(Entry);
2408 }
2409
2410 return Builder.CreateLoad(Entry, false, "tmp");
2411}
2412
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002413llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002414 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002415
2416 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002417 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002418 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002419 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002420 llvm::GlobalValue::InternalLinkage,
2421 C, "\01L_OBJC_CLASS_NAME_",
2422 &CGM.getModule());
2423 Entry->setSection("__TEXT,__cstring,cstring_literals");
2424 UsedGlobals.push_back(Entry);
2425 }
2426
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002427 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002428}
2429
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002430llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002431 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2432
2433 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002434 // FIXME: Avoid std::string copying.
2435 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002436 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002437 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002438 llvm::GlobalValue::InternalLinkage,
2439 C, "\01L_OBJC_METH_VAR_NAME_",
2440 &CGM.getModule());
2441 Entry->setSection("__TEXT,__cstring,cstring_literals");
2442 UsedGlobals.push_back(Entry);
2443 }
2444
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002445 return getConstantGEP(Entry, 0, 0);
2446}
2447
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002449llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2451}
2452
2453// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002454llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002455 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2456}
2457
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002458llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002460
2461 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002463 Entry =
2464 new llvm::GlobalVariable(C->getType(), false,
2465 llvm::GlobalValue::InternalLinkage,
2466 C, "\01L_OBJC_METH_VAR_TYPE_",
2467 &CGM.getModule());
2468 Entry->setSection("__TEXT,__cstring,cstring_literals");
2469 UsedGlobals.push_back(Entry);
2470 }
2471
2472 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002473}
2474
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002475// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002476llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002477 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002478 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2479 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 return GetMethodVarType(TypeStr);
2481}
2482
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002483// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002484llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002485 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2486
2487 if (!Entry) {
2488 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2489 Entry =
2490 new llvm::GlobalVariable(C->getType(), false,
2491 llvm::GlobalValue::InternalLinkage,
2492 C, "\01L_OBJC_PROP_NAME_ATTR_",
2493 &CGM.getModule());
2494 Entry->setSection("__TEXT,__cstring,cstring_literals");
2495 UsedGlobals.push_back(Entry);
2496 }
2497
2498 return getConstantGEP(Entry, 0, 0);
2499}
2500
2501// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002502// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002503llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002504 const Decl *Container) {
2505 std::string TypeStr;
2506 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002507 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2508}
2509
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002510void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2511 const ObjCContainerDecl *CD,
2512 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002513 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002514 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002515 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002516 assert (CD && "Missing container decl in GetNameForMethod");
2517 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002518 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2519 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002520 NameOut += ' ';
2521 NameOut += D->getSelector().getAsString();
2522 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002523}
2524
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002525/// GetFirstIvarInRecord - This routine returns the record for the
2526/// implementation of the fiven class OID. It also returns field
2527/// corresponding to the first ivar in the class in FIV. It also
2528/// returns the one before the first ivar.
2529///
2530const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2531 const ObjCInterfaceDecl *OID,
2532 RecordDecl::field_iterator &FIV,
2533 RecordDecl::field_iterator &PIV) {
2534 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2535 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2536 RecordDecl::field_iterator ifield = RD->field_begin();
2537 RecordDecl::field_iterator pfield = RD->field_end();
2538 while (countSuperClassIvars-- > 0) {
2539 pfield = ifield;
2540 ++ifield;
2541 }
2542 FIV = ifield;
2543 PIV = pfield;
2544 return RD;
2545}
2546
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002547void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002548 EmitModuleInfo();
2549
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002550 // Emit the dummy bodies for any protocols which were referenced but
2551 // never defined.
2552 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2553 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2554 if (i->second->hasInitializer())
2555 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002556
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002557 std::vector<llvm::Constant*> Values(5);
2558 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2559 Values[1] = GetClassName(i->first);
2560 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2561 Values[3] = Values[4] =
2562 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2563 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2564 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2565 Values));
2566 }
2567
2568 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002569 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002570 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002571 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002572 }
2573
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002574 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002575 llvm::GlobalValue *GV =
2576 new llvm::GlobalVariable(AT, false,
2577 llvm::GlobalValue::AppendingLinkage,
2578 llvm::ConstantArray::get(AT, Used),
2579 "llvm.used",
2580 &CGM.getModule());
2581
2582 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002583
2584 // Add assembler directives to add lazy undefined symbol references
2585 // for classes which are referenced but not defined. This is
2586 // important for correct linker interaction.
2587
2588 // FIXME: Uh, this isn't particularly portable.
2589 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002590
2591 if (!CGM.getModule().getModuleInlineAsm().empty())
2592 s << "\n";
2593
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002594 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2595 e = LazySymbols.end(); i != e; ++i) {
2596 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2597 }
2598 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2599 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002600 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002601 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2602 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002603
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002604 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002605}
2606
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002607CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002608 : CGObjCCommonMac(cgm),
2609 ObjCTypes(cgm)
2610{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002611 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002612 ObjCABI = 2;
2613}
2614
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002615/* *** */
2616
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002617ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2618: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002619{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002620 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2621 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002622
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002623 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002624 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002625 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002626 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2627
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002628 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002629 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002630 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002631
2632 // FIXME: It would be nice to unify this with the opaque type, so
2633 // that the IR comes out a bit cleaner.
2634 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2635 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002636
2637 // I'm not sure I like this. The implicit coordination is a bit
2638 // gross. We should solve this in a reasonable fashion because this
2639 // is a pretty common task (match some runtime data structure with
2640 // an LLVM data structure).
2641
2642 // FIXME: This is leaked.
2643 // FIXME: Merge with rewriter code?
2644
2645 // struct _objc_super {
2646 // id self;
2647 // Class cls;
2648 // }
2649 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2650 SourceLocation(),
2651 &Ctx.Idents.get("_objc_super"));
2652 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2653 Ctx.getObjCIdType(), 0, false));
2654 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2655 Ctx.getObjCClassType(), 0, false));
2656 RD->completeDefinition(Ctx);
2657
2658 SuperCTy = Ctx.getTagDeclType(RD);
2659 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2660
2661 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002662 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2663
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002664 // struct _prop_t {
2665 // char *name;
2666 // char *attributes;
2667 // }
2668 PropertyTy = llvm::StructType::get(Int8PtrTy,
2669 Int8PtrTy,
2670 NULL);
2671 CGM.getModule().addTypeName("struct._prop_t",
2672 PropertyTy);
2673
2674 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002675 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002676 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002677 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002678 // }
2679 PropertyListTy = llvm::StructType::get(IntTy,
2680 IntTy,
2681 llvm::ArrayType::get(PropertyTy, 0),
2682 NULL);
2683 CGM.getModule().addTypeName("struct._prop_list_t",
2684 PropertyListTy);
2685 // struct _prop_list_t *
2686 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2687
2688 // struct _objc_method {
2689 // SEL _cmd;
2690 // char *method_type;
2691 // char *_imp;
2692 // }
2693 MethodTy = llvm::StructType::get(SelectorPtrTy,
2694 Int8PtrTy,
2695 Int8PtrTy,
2696 NULL);
2697 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002698
2699 // struct _objc_cache *
2700 CacheTy = llvm::OpaqueType::get();
2701 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2702 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002703
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002704 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002705
2706 QualType IdType = Ctx.getObjCIdType();
2707 QualType SelType = Ctx.getObjCSelType();
2708 llvm::SmallVector<QualType,16> Params;
2709 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002710
2711 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002712 Params.push_back(IdType);
2713 Params.push_back(SelType);
2714 Params.push_back(Ctx.LongTy);
2715 Params.push_back(Ctx.BoolTy);
2716 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2717 false);
2718 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002719
2720 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2721 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002722 Params.push_back(IdType);
2723 Params.push_back(SelType);
2724 Params.push_back(Ctx.LongTy);
2725 Params.push_back(IdType);
2726 Params.push_back(Ctx.BoolTy);
2727 Params.push_back(Ctx.BoolTy);
2728 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2729 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2730
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002731 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002732
2733 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002734 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002735 Params.push_back(IdType);
2736 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2737 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2738 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002739
2740 // gc's API
2741 // id objc_read_weak (id *)
2742 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002743 Params.push_back(Ctx.getPointerType(IdType));
2744 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2745 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2746
2747 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002748 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002749 Params.push_back(IdType);
2750 Params.push_back(Ctx.getPointerType(IdType));
2751
2752 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2753 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2754 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2755 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2756 GcAssignStrongCastFn =
2757 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002758
2759 // void objc_exception_throw(id)
2760 Params.clear();
2761 Params.push_back(IdType);
2762
2763 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002764 ExceptionThrowFn =
2765 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002766
2767 // synchronized APIs
2768 // void objc_sync_enter (id)
2769 // void objc_sync_exit (id)
2770 Params.clear();
2771 Params.push_back(IdType);
2772
2773 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2774 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
2775 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002776}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002777
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002778ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2779 : ObjCCommonTypesHelper(cgm)
2780{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002781 // struct _objc_method_description {
2782 // SEL name;
2783 // char *types;
2784 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002785 MethodDescriptionTy =
2786 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002787 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002788 NULL);
2789 CGM.getModule().addTypeName("struct._objc_method_description",
2790 MethodDescriptionTy);
2791
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002792 // struct _objc_method_description_list {
2793 // int count;
2794 // struct _objc_method_description[1];
2795 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002796 MethodDescriptionListTy =
2797 llvm::StructType::get(IntTy,
2798 llvm::ArrayType::get(MethodDescriptionTy, 0),
2799 NULL);
2800 CGM.getModule().addTypeName("struct._objc_method_description_list",
2801 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002802
2803 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002804 MethodDescriptionListPtrTy =
2805 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2806
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002807 // Protocol description structures
2808
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002809 // struct _objc_protocol_extension {
2810 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2811 // struct _objc_method_description_list *optional_instance_methods;
2812 // struct _objc_method_description_list *optional_class_methods;
2813 // struct _objc_property_list *instance_properties;
2814 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002815 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002816 llvm::StructType::get(IntTy,
2817 MethodDescriptionListPtrTy,
2818 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002819 PropertyListPtrTy,
2820 NULL);
2821 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2822 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002823
2824 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002825 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2826
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002827 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002828
2829 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2830 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2831
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002832 const llvm::Type *T =
2833 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2834 LongTy,
2835 llvm::ArrayType::get(ProtocolTyHolder, 0),
2836 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002837 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2838
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002839 // struct _objc_protocol {
2840 // struct _objc_protocol_extension *isa;
2841 // char *protocol_name;
2842 // struct _objc_protocol **_objc_protocol_list;
2843 // struct _objc_method_description_list *instance_methods;
2844 // struct _objc_method_description_list *class_methods;
2845 // }
2846 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002847 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002848 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2849 MethodDescriptionListPtrTy,
2850 MethodDescriptionListPtrTy,
2851 NULL);
2852 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2853
2854 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2855 CGM.getModule().addTypeName("struct._objc_protocol_list",
2856 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002857 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002858 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2859
2860 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002861 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002862 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002863
2864 // Class description structures
2865
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002866 // struct _objc_ivar {
2867 // char *ivar_name;
2868 // char *ivar_type;
2869 // int ivar_offset;
2870 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002871 IvarTy = llvm::StructType::get(Int8PtrTy,
2872 Int8PtrTy,
2873 IntTy,
2874 NULL);
2875 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2876
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002877 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002878 IvarListTy = llvm::OpaqueType::get();
2879 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2880 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2881
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002882 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002883 MethodListTy = llvm::OpaqueType::get();
2884 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2885 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2886
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002887 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002888 ClassExtensionTy =
2889 llvm::StructType::get(IntTy,
2890 Int8PtrTy,
2891 PropertyListPtrTy,
2892 NULL);
2893 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2894 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2895
2896 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2897
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002898 // struct _objc_class {
2899 // Class isa;
2900 // Class super_class;
2901 // char *name;
2902 // long version;
2903 // long info;
2904 // long instance_size;
2905 // struct _objc_ivar_list *ivars;
2906 // struct _objc_method_list *methods;
2907 // struct _objc_cache *cache;
2908 // struct _objc_protocol_list *protocols;
2909 // char *ivar_layout;
2910 // struct _objc_class_ext *ext;
2911 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002912 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2913 llvm::PointerType::getUnqual(ClassTyHolder),
2914 Int8PtrTy,
2915 LongTy,
2916 LongTy,
2917 LongTy,
2918 IvarListPtrTy,
2919 MethodListPtrTy,
2920 CachePtrTy,
2921 ProtocolListPtrTy,
2922 Int8PtrTy,
2923 ClassExtensionPtrTy,
2924 NULL);
2925 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2926
2927 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2928 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2929 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2930
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002931 // struct _objc_category {
2932 // char *category_name;
2933 // char *class_name;
2934 // struct _objc_method_list *instance_method;
2935 // struct _objc_method_list *class_method;
2936 // uint32_t size; // sizeof(struct _objc_category)
2937 // struct _objc_property_list *instance_properties;// category's @property
2938 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002939 CategoryTy = llvm::StructType::get(Int8PtrTy,
2940 Int8PtrTy,
2941 MethodListPtrTy,
2942 MethodListPtrTy,
2943 ProtocolListPtrTy,
2944 IntTy,
2945 PropertyListPtrTy,
2946 NULL);
2947 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2948
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002949 // Global metadata structures
2950
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002951 // struct _objc_symtab {
2952 // long sel_ref_cnt;
2953 // SEL *refs;
2954 // short cls_def_cnt;
2955 // short cat_def_cnt;
2956 // char *defs[cls_def_cnt + cat_def_cnt];
2957 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002958 SymtabTy = llvm::StructType::get(LongTy,
2959 SelectorPtrTy,
2960 ShortTy,
2961 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002962 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002963 NULL);
2964 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2965 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2966
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002967 // struct _objc_module {
2968 // long version;
2969 // long size; // sizeof(struct _objc_module)
2970 // char *name;
2971 // struct _objc_symtab* symtab;
2972 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002973 ModuleTy =
2974 llvm::StructType::get(LongTy,
2975 LongTy,
2976 Int8PtrTy,
2977 SymtabPtrTy,
2978 NULL);
2979 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002980
Daniel Dunbar49f66022008-09-24 03:38:44 +00002981 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002982
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002983 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002984 std::vector<const llvm::Type*> Params;
2985 Params.push_back(ObjectPtrTy);
2986 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002987 MessageSendFn =
2988 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2989 Params,
2990 true),
2991 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002992
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002993 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002994 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002995 Params.push_back(ObjectPtrTy);
2996 Params.push_back(SelectorPtrTy);
2997 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002998 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2999 Params,
3000 true),
3001 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003002
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003003 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003004 Params.clear();
3005 Params.push_back(ObjectPtrTy);
3006 Params.push_back(SelectorPtrTy);
3007 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003008 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003009 MessageSendFpretFn =
3010 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3011 Params,
3012 true),
3013 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003014
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003015 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003016 Params.clear();
3017 Params.push_back(SuperPtrTy);
3018 Params.push_back(SelectorPtrTy);
3019 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003020 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3021 Params,
3022 true),
3023 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003024
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003025 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3026 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003027 Params.clear();
3028 Params.push_back(Int8PtrTy);
3029 Params.push_back(SuperPtrTy);
3030 Params.push_back(SelectorPtrTy);
3031 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003032 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3033 Params,
3034 true),
3035 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003036
3037 // There is no objc_msgSendSuper_fpret? How can that work?
3038 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003039
Anders Carlsson124526b2008-09-09 10:10:21 +00003040 // FIXME: This is the size of the setjmp buffer and should be
3041 // target specific. 18 is what's used on 32-bit X86.
3042 uint64_t SetJmpBufferSize = 18;
3043
3044 // Exceptions
3045 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003046 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003047
3048 ExceptionDataTy =
3049 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3050 SetJmpBufferSize),
3051 StackPtrTy, NULL);
3052 CGM.getModule().addTypeName("struct._objc_exception_data",
3053 ExceptionDataTy);
3054
3055 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003056 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3057 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003058 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3059 Params,
3060 false),
3061 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003062 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003063 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3064 Params,
3065 false),
3066 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003067 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003068 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3069 Params,
3070 false),
3071 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003072
3073 Params.clear();
3074 Params.push_back(ClassPtrTy);
3075 Params.push_back(ObjectPtrTy);
3076 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003077 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3078 Params,
3079 false),
3080 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003081
Anders Carlsson124526b2008-09-09 10:10:21 +00003082 Params.clear();
3083 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3084 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003085 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3086 Params,
3087 false),
3088 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003089
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003090}
3091
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003092ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003093: ObjCCommonTypesHelper(cgm)
3094{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003095 // struct _method_list_t {
3096 // uint32_t entsize; // sizeof(struct _objc_method)
3097 // uint32_t method_count;
3098 // struct _objc_method method_list[method_count];
3099 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003100 MethodListnfABITy = llvm::StructType::get(IntTy,
3101 IntTy,
3102 llvm::ArrayType::get(MethodTy, 0),
3103 NULL);
3104 CGM.getModule().addTypeName("struct.__method_list_t",
3105 MethodListnfABITy);
3106 // struct method_list_t *
3107 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003108
3109 // struct _protocol_t {
3110 // id isa; // NULL
3111 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003112 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003113 // const struct method_list_t * const instance_methods;
3114 // const struct method_list_t * const class_methods;
3115 // const struct method_list_t *optionalInstanceMethods;
3116 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003117 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003118 // const uint32_t size; // sizeof(struct _protocol_t)
3119 // const uint32_t flags; // = 0
3120 // }
3121
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003122 // Holder for struct _protocol_list_t *
3123 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3124
3125 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3126 Int8PtrTy,
3127 llvm::PointerType::getUnqual(
3128 ProtocolListTyHolder),
3129 MethodListnfABIPtrTy,
3130 MethodListnfABIPtrTy,
3131 MethodListnfABIPtrTy,
3132 MethodListnfABIPtrTy,
3133 PropertyListPtrTy,
3134 IntTy,
3135 IntTy,
3136 NULL);
3137 CGM.getModule().addTypeName("struct._protocol_t",
3138 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003139
3140 // struct _protocol_t*
3141 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003142
Fariborz Jahanianda320092009-01-29 19:24:30 +00003143 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003144 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003145 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003146 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003147 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3148 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003149 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003150 NULL);
3151 CGM.getModule().addTypeName("struct._objc_protocol_list",
3152 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003153 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3154 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003155
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003156 // struct _objc_protocol_list*
3157 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003158
3159 // struct _ivar_t {
3160 // unsigned long int *offset; // pointer to ivar offset location
3161 // char *name;
3162 // char *type;
3163 // uint32_t alignment;
3164 // uint32_t size;
3165 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003166 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3167 Int8PtrTy,
3168 Int8PtrTy,
3169 IntTy,
3170 IntTy,
3171 NULL);
3172 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3173
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003174 // struct _ivar_list_t {
3175 // uint32 entsize; // sizeof(struct _ivar_t)
3176 // uint32 count;
3177 // struct _iver_t list[count];
3178 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003179 IvarListnfABITy = llvm::StructType::get(IntTy,
3180 IntTy,
3181 llvm::ArrayType::get(
3182 IvarnfABITy, 0),
3183 NULL);
3184 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3185
3186 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003187
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003188 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003189 // uint32_t const flags;
3190 // uint32_t const instanceStart;
3191 // uint32_t const instanceSize;
3192 // uint32_t const reserved; // only when building for 64bit targets
3193 // const uint8_t * const ivarLayout;
3194 // const char *const name;
3195 // const struct _method_list_t * const baseMethods;
3196 // const struct _objc_protocol_list *const baseProtocols;
3197 // const struct _ivar_list_t *const ivars;
3198 // const uint8_t * const weakIvarLayout;
3199 // const struct _prop_list_t * const properties;
3200 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003201
3202 // FIXME. Add 'reserved' field in 64bit abi mode!
3203 ClassRonfABITy = llvm::StructType::get(IntTy,
3204 IntTy,
3205 IntTy,
3206 Int8PtrTy,
3207 Int8PtrTy,
3208 MethodListnfABIPtrTy,
3209 ProtocolListnfABIPtrTy,
3210 IvarListnfABIPtrTy,
3211 Int8PtrTy,
3212 PropertyListPtrTy,
3213 NULL);
3214 CGM.getModule().addTypeName("struct._class_ro_t",
3215 ClassRonfABITy);
3216
3217 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3218 std::vector<const llvm::Type*> Params;
3219 Params.push_back(ObjectPtrTy);
3220 Params.push_back(SelectorPtrTy);
3221 ImpnfABITy = llvm::PointerType::getUnqual(
3222 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3223
3224 // struct _class_t {
3225 // struct _class_t *isa;
3226 // struct _class_t * const superclass;
3227 // void *cache;
3228 // IMP *vtable;
3229 // struct class_ro_t *ro;
3230 // }
3231
3232 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3233 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3234 llvm::PointerType::getUnqual(ClassTyHolder),
3235 CachePtrTy,
3236 llvm::PointerType::getUnqual(ImpnfABITy),
3237 llvm::PointerType::getUnqual(
3238 ClassRonfABITy),
3239 NULL);
3240 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3241
3242 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3243 ClassnfABITy);
3244
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003245 // LLVM for struct _class_t *
3246 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3247
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003248 // struct _category_t {
3249 // const char * const name;
3250 // struct _class_t *const cls;
3251 // const struct _method_list_t * const instance_methods;
3252 // const struct _method_list_t * const class_methods;
3253 // const struct _protocol_list_t * const protocols;
3254 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003255 // }
3256 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003257 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003258 MethodListnfABIPtrTy,
3259 MethodListnfABIPtrTy,
3260 ProtocolListnfABIPtrTy,
3261 PropertyListPtrTy,
3262 NULL);
3263 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003264
3265 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003266 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3267 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003268
3269 // MessageRefTy - LLVM for:
3270 // struct _message_ref_t {
3271 // IMP messenger;
3272 // SEL name;
3273 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003274
3275 // First the clang type for struct _message_ref_t
3276 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3277 SourceLocation(),
3278 &Ctx.Idents.get("_message_ref_t"));
3279 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3280 Ctx.VoidPtrTy, 0, false));
3281 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3282 Ctx.getObjCSelType(), 0, false));
3283 RD->completeDefinition(Ctx);
3284
3285 MessageRefCTy = Ctx.getTagDeclType(RD);
3286 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3287 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003288
3289 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3290 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3291
3292 // SuperMessageRefTy - LLVM for:
3293 // struct _super_message_ref_t {
3294 // SUPER_IMP messenger;
3295 // SEL name;
3296 // };
3297 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3298 SelectorPtrTy,
3299 NULL);
3300 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3301
3302 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3303 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3304
3305 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3306 Params.clear();
3307 Params.push_back(ObjectPtrTy);
3308 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003309 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3310 Params,
3311 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003312 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003313 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003314 "objc_msgSend_fixup");
3315
3316 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3317 MessageSendFpretFixupFn =
3318 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3319 Params,
3320 true),
3321 "objc_msgSend_fpret_fixup");
3322
3323 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3324 MessageSendStretFixupFn =
3325 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3326 Params,
3327 true),
3328 "objc_msgSend_stret_fixup");
3329
3330 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3331 MessageSendIdFixupFn =
3332 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3333 Params,
3334 true),
3335 "objc_msgSendId_fixup");
3336
3337
3338 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3339 MessageSendIdStretFixupFn =
3340 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3341 Params,
3342 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003343 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003344
3345 // id objc_msgSendSuper2_fixup (struct objc_super *,
3346 // struct _super_message_ref_t*, ...)
3347 Params.clear();
3348 Params.push_back(SuperPtrTy);
3349 Params.push_back(SuperMessageRefPtrTy);
3350 MessageSendSuper2FixupFn =
3351 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3352 Params,
3353 true),
3354 "objc_msgSendSuper2_fixup");
3355
3356
3357 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3358 // struct _super_message_ref_t*, ...)
3359 MessageSendSuper2StretFixupFn =
3360 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3361 Params,
3362 true),
3363 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003364
3365 Params.clear();
3366 llvm::Constant *Personality =
3367 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3368 Params,
3369 true),
3370 "__objc_personality_v0");
3371 EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
3372
3373 Params.clear();
3374 Params.push_back(Int8PtrTy);
3375 UnwindResumeOrRethrowFn =
3376 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3377 Params,
3378 false),
3379 "_Unwind_Resume_or_Rethrow");
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003380}
3381
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003382llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3383 FinishNonFragileABIModule();
3384
3385 return NULL;
3386}
3387
3388void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3389 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003390
3391 // Build list of all implemented classe addresses in array
3392 // L_OBJC_LABEL_CLASS_$.
3393 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3394 // list of 'nonlazy' implementations (defined as those with a +load{}
3395 // method!!).
3396 unsigned NumClasses = DefinedClasses.size();
3397 if (NumClasses) {
3398 std::vector<llvm::Constant*> Symbols(NumClasses);
3399 for (unsigned i=0; i<NumClasses; i++)
3400 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3401 ObjCTypes.Int8PtrTy);
3402 llvm::Constant* Init =
3403 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3404 NumClasses),
3405 Symbols);
3406
3407 llvm::GlobalVariable *GV =
3408 new llvm::GlobalVariable(Init->getType(), false,
3409 llvm::GlobalValue::InternalLinkage,
3410 Init,
3411 "\01L_OBJC_LABEL_CLASS_$",
3412 &CGM.getModule());
Fariborz Jahanian5a1edf62009-02-28 00:54:00 +00003413 GV->setAlignment(
3414 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003415 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3416 UsedGlobals.push_back(GV);
3417 }
3418
3419 // Build list of all implemented category addresses in array
3420 // L_OBJC_LABEL_CATEGORY_$.
3421 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3422 // list of 'nonlazy' category implementations (defined as those with a +load{}
3423 // method!!).
3424 unsigned NumCategory = DefinedCategories.size();
3425 if (NumCategory) {
3426 std::vector<llvm::Constant*> Symbols(NumCategory);
3427 for (unsigned i=0; i<NumCategory; i++)
3428 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3429 ObjCTypes.Int8PtrTy);
3430 llvm::Constant* Init =
3431 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3432 NumCategory),
3433 Symbols);
3434
3435 llvm::GlobalVariable *GV =
3436 new llvm::GlobalVariable(Init->getType(), false,
3437 llvm::GlobalValue::InternalLinkage,
3438 Init,
3439 "\01L_OBJC_LABEL_CATEGORY_$",
3440 &CGM.getModule());
3441 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3442 UsedGlobals.push_back(GV);
3443 }
3444
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003445 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3446 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3447 std::vector<llvm::Constant*> Values(2);
3448 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003449 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00003450 // FIXME: Fix and continue?
3451 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3452 flags |= eImageInfo_GarbageCollected;
3453 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3454 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003455 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003456 llvm::Constant* Init = llvm::ConstantArray::get(
3457 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3458 Values);
3459 llvm::GlobalVariable *IMGV =
3460 new llvm::GlobalVariable(Init->getType(), false,
3461 llvm::GlobalValue::InternalLinkage,
3462 Init,
3463 "\01L_OBJC_IMAGE_INFO",
3464 &CGM.getModule());
3465 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3466 UsedGlobals.push_back(IMGV);
3467
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003468 std::vector<llvm::Constant*> Used;
3469 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3470 e = UsedGlobals.end(); i != e; ++i) {
3471 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3472 }
3473
3474 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3475 llvm::GlobalValue *GV =
3476 new llvm::GlobalVariable(AT, false,
3477 llvm::GlobalValue::AppendingLinkage,
3478 llvm::ConstantArray::get(AT, Used),
3479 "llvm.used",
3480 &CGM.getModule());
3481
3482 GV->setSection("llvm.metadata");
3483
3484}
3485
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003486// Metadata flags
3487enum MetaDataDlags {
3488 CLS = 0x0,
3489 CLS_META = 0x1,
3490 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003491 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003492 CLS_EXCEPTION = 0x20
3493};
3494/// BuildClassRoTInitializer - generate meta-data for:
3495/// struct _class_ro_t {
3496/// uint32_t const flags;
3497/// uint32_t const instanceStart;
3498/// uint32_t const instanceSize;
3499/// uint32_t const reserved; // only when building for 64bit targets
3500/// const uint8_t * const ivarLayout;
3501/// const char *const name;
3502/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003503/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003504/// const struct _ivar_list_t *const ivars;
3505/// const uint8_t * const weakIvarLayout;
3506/// const struct _prop_list_t * const properties;
3507/// }
3508///
3509llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3510 unsigned flags,
3511 unsigned InstanceStart,
3512 unsigned InstanceSize,
3513 const ObjCImplementationDecl *ID) {
3514 std::string ClassName = ID->getNameAsString();
3515 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3516 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3517 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3518 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3519 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003520 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003521 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3522 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003523 // const struct _method_list_t * const baseMethods;
3524 std::vector<llvm::Constant*> Methods;
3525 std::string MethodListName("\01l_OBJC_$_");
3526 if (flags & CLS_META) {
3527 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3528 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3529 e = ID->classmeth_end(); i != e; ++i) {
3530 // Class methods should always be defined.
3531 Methods.push_back(GetMethodConstant(*i));
3532 }
3533 } else {
3534 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3535 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3536 e = ID->instmeth_end(); i != e; ++i) {
3537 // Instance methods should always be defined.
3538 Methods.push_back(GetMethodConstant(*i));
3539 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003540 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3541 e = ID->propimpl_end(); i != e; ++i) {
3542 ObjCPropertyImplDecl *PID = *i;
3543
3544 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3545 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3546
3547 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3548 if (llvm::Constant *C = GetMethodConstant(MD))
3549 Methods.push_back(C);
3550 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3551 if (llvm::Constant *C = GetMethodConstant(MD))
3552 Methods.push_back(C);
3553 }
3554 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003555 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003556 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003557 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003558
3559 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3560 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3561 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3562 + OID->getNameAsString(),
3563 OID->protocol_begin(),
3564 OID->protocol_end());
3565
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003566 if (flags & CLS_META)
3567 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3568 else
3569 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003570 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003571 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003572 if (flags & CLS_META)
3573 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3574 else
3575 Values[ 9] =
3576 EmitPropertyList(
3577 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3578 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003579 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3580 Values);
3581 llvm::GlobalVariable *CLASS_RO_GV =
3582 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3583 llvm::GlobalValue::InternalLinkage,
3584 Init,
3585 (flags & CLS_META) ?
3586 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3587 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3588 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003589 CLASS_RO_GV->setAlignment(
3590 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003591 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003592 UsedGlobals.push_back(CLASS_RO_GV);
3593 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003594
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003595}
3596
3597/// BuildClassMetaData - This routine defines that to-level meta-data
3598/// for the given ClassName for:
3599/// struct _class_t {
3600/// struct _class_t *isa;
3601/// struct _class_t * const superclass;
3602/// void *cache;
3603/// IMP *vtable;
3604/// struct class_ro_t *ro;
3605/// }
3606///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003607llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3608 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003609 llvm::Constant *IsAGV,
3610 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003611 llvm::Constant *ClassRoGV,
3612 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003613 std::vector<llvm::Constant*> Values(5);
3614 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003615 Values[1] = SuperClassGV
3616 ? SuperClassGV
3617 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003618 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3619 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3620 Values[4] = ClassRoGV; // &CLASS_RO_GV
3621 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3622 Values);
3623 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3624 if (GV)
3625 GV->setInitializer(Init);
3626 else
3627 GV =
3628 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3629 llvm::GlobalValue::ExternalLinkage,
3630 Init,
3631 ClassName,
3632 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003633 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003634 GV->setAlignment(
3635 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003636 if (HiddenVisibility)
3637 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003638 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003639 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003640}
3641
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003642void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3643 std::string ClassName = ID->getNameAsString();
3644 if (!ObjCEmptyCacheVar) {
3645 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003646 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003647 false,
3648 llvm::GlobalValue::ExternalLinkage,
3649 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003650 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003651 &CGM.getModule());
3652 UsedGlobals.push_back(ObjCEmptyCacheVar);
3653
3654 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003655 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003656 false,
3657 llvm::GlobalValue::ExternalLinkage,
3658 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003659 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003660 &CGM.getModule());
3661 UsedGlobals.push_back(ObjCEmptyVtableVar);
3662 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003663 assert(ID->getClassInterface() &&
3664 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003665 uint32_t InstanceStart =
3666 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3667 uint32_t InstanceSize = InstanceStart;
3668 uint32_t flags = CLS_META;
3669 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3670 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003671
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003672 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003673
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003674 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3675 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003676 flags |= OBJC2_CLS_HIDDEN;
3677 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003678 // class is root
3679 flags |= CLS_ROOT;
3680 std::string SuperClassName = ObjCClassName + ClassName;
3681 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003682 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003683 SuperClassGV =
3684 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3685 llvm::GlobalValue::ExternalLinkage,
3686 0,
3687 SuperClassName,
3688 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003689 UsedGlobals.push_back(SuperClassGV);
3690 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003691 std::string IsAClassName = ObjCMetaClassName + ClassName;
3692 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003693 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003694 IsAGV =
3695 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3696 llvm::GlobalValue::ExternalLinkage,
3697 0,
3698 IsAClassName,
3699 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003700 UsedGlobals.push_back(IsAGV);
3701 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003702 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003703 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00003704 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3705 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3706 Root = Super;
3707 std::string RootClassName = ObjCMetaClassName + Root->getNameAsString();
3708 IsAGV = CGM.getModule().getGlobalVariable(RootClassName);
3709 if (!IsAGV) {
3710 IsAGV =
3711 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3712 llvm::GlobalValue::ExternalLinkage,
3713 0,
3714 RootClassName,
3715 &CGM.getModule());
3716 UsedGlobals.push_back(IsAGV);
3717 }
3718 // work on super class metadata symbol.
3719 std::string SuperClassName =
3720 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003721 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003722 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003723 SuperClassGV =
3724 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3725 llvm::GlobalValue::ExternalLinkage,
3726 0,
3727 SuperClassName,
3728 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003729 UsedGlobals.push_back(SuperClassGV);
3730 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003731 }
3732 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3733 InstanceStart,
3734 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003735 std::string TClassName = ObjCMetaClassName + ClassName;
3736 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003737 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3738 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003739
3740 // Metadata for the class
3741 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003742 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003743 flags |= OBJC2_CLS_HIDDEN;
3744 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003745 flags |= CLS_ROOT;
3746 SuperClassGV = 0;
3747 }
3748 else {
3749 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00003750 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003751 ID->getClassInterface()->getSuperClass()->getNameAsString();
3752 std::string SuperClassName = ObjCClassName + RootClassName;
3753 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003754 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003755 SuperClassGV =
3756 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3757 llvm::GlobalValue::ExternalLinkage,
3758 0,
3759 SuperClassName,
3760 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003761 UsedGlobals.push_back(SuperClassGV);
3762 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003763 }
3764
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003765 InstanceStart = InstanceSize = 0;
3766 if (ObjCInterfaceDecl *OID =
3767 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3768 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003769 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003770 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003771 const llvm::StructLayout *Layout =
3772 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003773
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003774 RecordDecl::field_iterator firstField, lastField;
3775 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003776
3777 for (RecordDecl::field_iterator e = RD->field_end(),
3778 ifield = firstField; ifield != e; ++ifield)
3779 lastField = ifield;
3780
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003781 if (lastField != RD->field_end()) {
3782 FieldDecl *Field = *lastField;
3783 const llvm::Type *FieldTy =
3784 CGM.getTypes().ConvertTypeForMem(Field->getType());
3785 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3786 InstanceSize = Layout->getElementOffset(
3787 CGM.getTypes().getLLVMFieldNo(Field)) +
3788 Size;
3789 if (firstField == RD->field_end())
3790 InstanceStart = InstanceSize;
3791 else
3792 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3793 getLLVMFieldNo(*firstField));
3794 }
3795 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003796 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003797 InstanceStart,
3798 InstanceSize,
3799 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003800
3801 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003802 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003803 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3804 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003805 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003806}
3807
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003808/// GenerateProtocolRef - This routine is called to generate code for
3809/// a protocol reference expression; as in:
3810/// @code
3811/// @protocol(Proto1);
3812/// @endcode
3813/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3814/// which will hold address of the protocol meta-data.
3815///
3816llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3817 const ObjCProtocolDecl *PD) {
3818
3819 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3820 ObjCTypes.ExternalProtocolPtrTy);
3821
3822 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3823 ProtocolName += PD->getNameAsCString();
3824
3825 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3826 if (PTGV)
3827 return Builder.CreateLoad(PTGV, false, "tmp");
3828 PTGV = new llvm::GlobalVariable(
3829 Init->getType(), false,
3830 llvm::GlobalValue::WeakLinkage,
3831 Init,
3832 ProtocolName,
3833 &CGM.getModule());
3834 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3835 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3836 UsedGlobals.push_back(PTGV);
3837 return Builder.CreateLoad(PTGV, false, "tmp");
3838}
3839
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003840/// GenerateCategory - Build metadata for a category implementation.
3841/// struct _category_t {
3842/// const char * const name;
3843/// struct _class_t *const cls;
3844/// const struct _method_list_t * const instance_methods;
3845/// const struct _method_list_t * const class_methods;
3846/// const struct _protocol_list_t * const protocols;
3847/// const struct _prop_list_t * const properties;
3848/// }
3849///
3850void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3851{
3852 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003853 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3854 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003855 "_$_" + OCD->getNameAsString());
3856 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3857
3858 std::vector<llvm::Constant*> Values(6);
3859 Values[0] = GetClassName(OCD->getIdentifier());
3860 // meta-class entry symbol
3861 llvm::GlobalVariable *ClassGV =
3862 CGM.getModule().getGlobalVariable(ExtClassName);
3863 if (!ClassGV)
3864 ClassGV =
3865 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3866 llvm::GlobalValue::ExternalLinkage,
3867 0,
3868 ExtClassName,
3869 &CGM.getModule());
3870 UsedGlobals.push_back(ClassGV);
3871 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003872 std::vector<llvm::Constant*> Methods;
3873 std::string MethodListName(Prefix);
3874 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3875 "_$_" + OCD->getNameAsString();
3876
3877 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3878 e = OCD->instmeth_end(); i != e; ++i) {
3879 // Instance methods should always be defined.
3880 Methods.push_back(GetMethodConstant(*i));
3881 }
3882
3883 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003884 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003885 Methods);
3886
3887 MethodListName = Prefix;
3888 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3889 OCD->getNameAsString();
3890 Methods.clear();
3891 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3892 e = OCD->classmeth_end(); i != e; ++i) {
3893 // Class methods should always be defined.
3894 Methods.push_back(GetMethodConstant(*i));
3895 }
3896
3897 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003898 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003899 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003900 const ObjCCategoryDecl *Category =
3901 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003902 if (Category) {
3903 std::string ExtName(Interface->getNameAsString() + "_$_" +
3904 OCD->getNameAsString());
3905 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3906 + Interface->getNameAsString() + "_$_"
3907 + Category->getNameAsString(),
3908 Category->protocol_begin(),
3909 Category->protocol_end());
3910 Values[5] =
3911 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3912 OCD, Category, ObjCTypes);
3913 }
3914 else {
3915 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3916 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3917 }
3918
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003919 llvm::Constant *Init =
3920 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3921 Values);
3922 llvm::GlobalVariable *GCATV
3923 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3924 false,
3925 llvm::GlobalValue::InternalLinkage,
3926 Init,
3927 ExtCatName,
3928 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003929 GCATV->setAlignment(
3930 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003931 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003932 UsedGlobals.push_back(GCATV);
3933 DefinedCategories.push_back(GCATV);
3934}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003935
3936/// GetMethodConstant - Return a struct objc_method constant for the
3937/// given method if it has been defined. The result is null if the
3938/// method has not been defined. The return value has type MethodPtrTy.
3939llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3940 const ObjCMethodDecl *MD) {
3941 // FIXME: Use DenseMap::lookup
3942 llvm::Function *Fn = MethodDefinitions[MD];
3943 if (!Fn)
3944 return 0;
3945
3946 std::vector<llvm::Constant*> Method(3);
3947 Method[0] =
3948 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3949 ObjCTypes.SelectorPtrTy);
3950 Method[1] = GetMethodVarType(MD);
3951 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3952 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3953}
3954
3955/// EmitMethodList - Build meta-data for method declarations
3956/// struct _method_list_t {
3957/// uint32_t entsize; // sizeof(struct _objc_method)
3958/// uint32_t method_count;
3959/// struct _objc_method method_list[method_count];
3960/// }
3961///
3962llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3963 const std::string &Name,
3964 const char *Section,
3965 const ConstantVector &Methods) {
3966 // Return null for empty list.
3967 if (Methods.empty())
3968 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3969
3970 std::vector<llvm::Constant*> Values(3);
3971 // sizeof(struct _objc_method)
3972 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3973 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3974 // method_count
3975 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3976 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3977 Methods.size());
3978 Values[2] = llvm::ConstantArray::get(AT, Methods);
3979 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3980
3981 llvm::GlobalVariable *GV =
3982 new llvm::GlobalVariable(Init->getType(), false,
3983 llvm::GlobalValue::InternalLinkage,
3984 Init,
3985 Name,
3986 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003987 GV->setAlignment(
3988 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003989 GV->setSection(Section);
3990 UsedGlobals.push_back(GV);
3991 return llvm::ConstantExpr::getBitCast(GV,
3992 ObjCTypes.MethodListnfABIPtrTy);
3993}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003994
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003995/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3996/// the given ivar.
3997///
3998llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3999 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004000 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004001 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004002 Name += "\01_OBJC_IVAR_$_" +
4003 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004004 + Ivar->getNameAsString();
4005 llvm::GlobalVariable *IvarOffsetGV =
4006 CGM.getModule().getGlobalVariable(Name);
4007 if (!IvarOffsetGV)
4008 IvarOffsetGV =
4009 new llvm::GlobalVariable(ObjCTypes.LongTy,
4010 false,
4011 llvm::GlobalValue::ExternalLinkage,
4012 0,
4013 Name,
4014 &CGM.getModule());
4015 return IvarOffsetGV;
4016}
4017
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004018llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004019 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004020 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004021 unsigned long int Offset) {
4022
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004023 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004024 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004025 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004026 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004027
4028 llvm::GlobalVariable *IvarOffsetGV =
4029 CGM.getModule().getGlobalVariable(ExternalName);
4030 if (IvarOffsetGV) {
4031 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004032 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004033 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004034 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004035 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004036 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004037 return IvarOffsetGV;
4038 }
4039
4040 IvarOffsetGV =
4041 new llvm::GlobalVariable(Init->getType(),
4042 false,
4043 llvm::GlobalValue::ExternalLinkage,
4044 Init,
4045 ExternalName,
4046 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004047 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004048 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004049 // @private and @package have hidden visibility.
4050 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4051 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4052 if (!globalVisibility)
4053 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004054 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004055 if (IsClassHidden(ID))
4056 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004057
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004058 IvarOffsetGV->setSection("__DATA, __objc_const");
4059 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004060 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004061}
4062
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004063/// EmitIvarList - Emit the ivar list for the given
4064/// implementation. If ForClass is true the list of class ivars
4065/// (i.e. metaclass ivars) is emitted, otherwise the list of
4066/// interface ivars will be emitted. The return value has type
4067/// IvarListnfABIPtrTy.
4068/// struct _ivar_t {
4069/// unsigned long int *offset; // pointer to ivar offset location
4070/// char *name;
4071/// char *type;
4072/// uint32_t alignment;
4073/// uint32_t size;
4074/// }
4075/// struct _ivar_list_t {
4076/// uint32 entsize; // sizeof(struct _ivar_t)
4077/// uint32 count;
4078/// struct _iver_t list[count];
4079/// }
4080///
4081llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4082 const ObjCImplementationDecl *ID) {
4083
4084 std::vector<llvm::Constant*> Ivars, Ivar(5);
4085
4086 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4087 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4088
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004089 // FIXME. Consolidate this with similar code in GenerateClass.
4090 const llvm::Type *InterfaceTy =
4091 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4092 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004093 const llvm::StructLayout *Layout =
4094 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004095
4096 RecordDecl::field_iterator i,p;
4097 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004098 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4099
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004100 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004101 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004102 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4103 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004104 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004105 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004106 if (Field->getIdentifier())
4107 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4108 else
4109 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4110 std::string TypeStr;
4111 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4112 Ivar[2] = GetMethodVarType(TypeStr);
4113 const llvm::Type *FieldTy =
4114 CGM.getTypes().ConvertTypeForMem(Field->getType());
4115 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4116 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4117 Field->getType().getTypePtr()) >> 3;
4118 Align = llvm::Log2_32(Align);
4119 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004120 // NOTE. Size of a bitfield does not match gcc's, because of the way
4121 // bitfields are treated special in each. But I am told that 'size'
4122 // for bitfield ivars is ignored by the runtime so it does not matter.
4123 // (even if it matters, some day, there is enough info. to get the bitfield
4124 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004125 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4126 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4127 }
4128 // Return null for empty list.
4129 if (Ivars.empty())
4130 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4131 std::vector<llvm::Constant*> Values(3);
4132 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4133 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4134 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4135 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4136 Ivars.size());
4137 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4138 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4139 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4140 llvm::GlobalVariable *GV =
4141 new llvm::GlobalVariable(Init->getType(), false,
4142 llvm::GlobalValue::InternalLinkage,
4143 Init,
4144 Prefix + OID->getNameAsString(),
4145 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004146 GV->setAlignment(
4147 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004148 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004149
4150 UsedGlobals.push_back(GV);
4151 return llvm::ConstantExpr::getBitCast(GV,
4152 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004153}
4154
4155llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4156 const ObjCProtocolDecl *PD) {
4157 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4158
4159 if (!Entry) {
4160 // We use the initializer as a marker of whether this is a forward
4161 // reference or not. At module finalization we add the empty
4162 // contents for protocols which were referenced but never defined.
4163 Entry =
4164 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4165 llvm::GlobalValue::ExternalLinkage,
4166 0,
4167 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4168 &CGM.getModule());
4169 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4170 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004171 }
4172
4173 return Entry;
4174}
4175
4176/// GetOrEmitProtocol - Generate the protocol meta-data:
4177/// @code
4178/// struct _protocol_t {
4179/// id isa; // NULL
4180/// const char * const protocol_name;
4181/// const struct _protocol_list_t * protocol_list; // super protocols
4182/// const struct method_list_t * const instance_methods;
4183/// const struct method_list_t * const class_methods;
4184/// const struct method_list_t *optionalInstanceMethods;
4185/// const struct method_list_t *optionalClassMethods;
4186/// const struct _prop_list_t * properties;
4187/// const uint32_t size; // sizeof(struct _protocol_t)
4188/// const uint32_t flags; // = 0
4189/// }
4190/// @endcode
4191///
4192
4193llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4194 const ObjCProtocolDecl *PD) {
4195 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4196
4197 // Early exit if a defining object has already been generated.
4198 if (Entry && Entry->hasInitializer())
4199 return Entry;
4200
4201 const char *ProtocolName = PD->getNameAsCString();
4202
4203 // Construct method lists.
4204 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4205 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4206 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4207 e = PD->instmeth_end(); i != e; ++i) {
4208 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004209 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004210 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4211 OptInstanceMethods.push_back(C);
4212 } else {
4213 InstanceMethods.push_back(C);
4214 }
4215 }
4216
4217 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4218 e = PD->classmeth_end(); i != e; ++i) {
4219 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004220 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004221 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4222 OptClassMethods.push_back(C);
4223 } else {
4224 ClassMethods.push_back(C);
4225 }
4226 }
4227
4228 std::vector<llvm::Constant*> Values(10);
4229 // isa is NULL
4230 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4231 Values[1] = GetClassName(PD->getIdentifier());
4232 Values[2] = EmitProtocolList(
4233 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4234 PD->protocol_begin(),
4235 PD->protocol_end());
4236
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004237 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004238 + PD->getNameAsString(),
4239 "__DATA, __objc_const",
4240 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004241 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004242 + PD->getNameAsString(),
4243 "__DATA, __objc_const",
4244 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004245 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004246 + PD->getNameAsString(),
4247 "__DATA, __objc_const",
4248 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004249 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004250 + PD->getNameAsString(),
4251 "__DATA, __objc_const",
4252 OptClassMethods);
4253 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4254 0, PD, ObjCTypes);
4255 uint32_t Size =
4256 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4257 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4258 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4259 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4260 Values);
4261
4262 if (Entry) {
4263 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004264 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004265 Entry->setInitializer(Init);
4266 } else {
4267 Entry =
4268 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004269 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004270 Init,
4271 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4272 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004273 Entry->setAlignment(
4274 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004275 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004276 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004277 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4278
4279 // Use this protocol meta-data to build protocol list table in section
4280 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004281 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004282 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004283 llvm::GlobalValue::WeakLinkage,
4284 Entry,
4285 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4286 +ProtocolName,
4287 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004288 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004289 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004290 PTGV->setSection("__DATA, __objc_protolist");
4291 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4292 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004293 return Entry;
4294}
4295
4296/// EmitProtocolList - Generate protocol list meta-data:
4297/// @code
4298/// struct _protocol_list_t {
4299/// long protocol_count; // Note, this is 32/64 bit
4300/// struct _protocol_t[protocol_count];
4301/// }
4302/// @endcode
4303///
4304llvm::Constant *
4305CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4306 ObjCProtocolDecl::protocol_iterator begin,
4307 ObjCProtocolDecl::protocol_iterator end) {
4308 std::vector<llvm::Constant*> ProtocolRefs;
4309
Fariborz Jahanianda320092009-01-29 19:24:30 +00004310 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004311 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004312 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4313
Daniel Dunbar948e2582009-02-15 07:36:20 +00004314 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004315 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4316 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004317 return llvm::ConstantExpr::getBitCast(GV,
4318 ObjCTypes.ProtocolListnfABIPtrTy);
4319
4320 for (; begin != end; ++begin)
4321 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4322
Fariborz Jahanianda320092009-01-29 19:24:30 +00004323 // This list is null terminated.
4324 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004325 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004326
4327 std::vector<llvm::Constant*> Values(2);
4328 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4329 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004330 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004331 ProtocolRefs.size()),
4332 ProtocolRefs);
4333
4334 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4335 GV = new llvm::GlobalVariable(Init->getType(), false,
4336 llvm::GlobalValue::InternalLinkage,
4337 Init,
4338 Name,
4339 &CGM.getModule());
4340 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004341 GV->setAlignment(
4342 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004343 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004344 return llvm::ConstantExpr::getBitCast(GV,
4345 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004346}
4347
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004348/// GetMethodDescriptionConstant - This routine build following meta-data:
4349/// struct _objc_method {
4350/// SEL _cmd;
4351/// char *method_type;
4352/// char *_imp;
4353/// }
4354
4355llvm::Constant *
4356CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4357 std::vector<llvm::Constant*> Desc(3);
4358 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4359 ObjCTypes.SelectorPtrTy);
4360 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004361 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004362 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4363 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4364}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004365
4366/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4367/// This code gen. amounts to generating code for:
4368/// @code
4369/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4370/// @encode
4371///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004372LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004373 CodeGen::CodeGenFunction &CGF,
4374 QualType ObjectTy,
4375 llvm::Value *BaseValue,
4376 const ObjCIvarDecl *Ivar,
4377 const FieldDecl *Field,
4378 unsigned CVRQualifiers) {
4379 assert(ObjectTy->isObjCInterfaceType() &&
4380 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004381 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004382 std::string ExternalName;
4383 llvm::GlobalVariable *IvarOffsetGV =
4384 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004385
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004386 // (char *) BaseValue
4387 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4388 ObjCTypes.Int8PtrTy);
4389 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4390 // (char*)BaseValue + Offset_symbol
4391 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4392 // (type *)((char*)BaseValue + Offset_symbol)
4393 const llvm::Type *IvarTy =
4394 CGM.getTypes().ConvertType(Ivar->getType());
4395 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4396 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004397
4398 if (Ivar->isBitField())
4399 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4400 CVRQualifiers);
4401
4402 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004403 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4404 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004405 LValue::SetObjCIvar(LV, true);
4406 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004407}
4408
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004409llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4410 CodeGen::CodeGenFunction &CGF,
4411 ObjCInterfaceDecl *Interface,
4412 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004413 std::string ExternalName;
4414 llvm::GlobalVariable *IvarOffsetGV =
4415 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4416
4417 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004418}
4419
Fariborz Jahanian46551122009-02-04 00:22:57 +00004420CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4421 CodeGen::CodeGenFunction &CGF,
4422 QualType ResultType,
4423 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004424 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004425 QualType Arg0Ty,
4426 bool IsSuper,
4427 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004428 // FIXME. Even though IsSuper is passes. This function doese not
4429 // handle calls to 'super' receivers.
4430 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004431 llvm::Value *Arg0 = Receiver;
4432 if (!IsSuper)
4433 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004434
4435 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004436 // FIXME. This is too much work to get the ABI-specific result type
4437 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004438 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4439 llvm::SmallVector<QualType, 16>());
4440 llvm::Constant *Fn;
4441 std::string Name("\01l_");
4442 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004443#if 0
4444 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004445 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4446 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4447 // FIXME. Is there a better way of getting these names.
4448 // They are available in RuntimeFunctions vector pair.
4449 Name += "objc_msgSendId_stret_fixup";
4450 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004451 else
4452#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004453 if (IsSuper) {
4454 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4455 Name += "objc_msgSendSuper2_stret_fixup";
4456 }
4457 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004458 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004459 Fn = ObjCTypes.MessageSendStretFixupFn;
4460 Name += "objc_msgSend_stret_fixup";
4461 }
4462 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004463 else if (ResultType->isFloatingType() &&
4464 // Selection of frret API only happens in 32bit nonfragile ABI.
4465 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004466 Fn = ObjCTypes.MessageSendFpretFixupFn;
4467 Name += "objc_msgSend_fpret_fixup";
4468 }
4469 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004470#if 0
4471// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004472 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4473 Fn = ObjCTypes.MessageSendIdFixupFn;
4474 Name += "objc_msgSendId_fixup";
4475 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004476 else
4477#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004478 if (IsSuper) {
4479 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4480 Name += "objc_msgSendSuper2_fixup";
4481 }
4482 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004483 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004484 Fn = ObjCTypes.MessageSendFixupFn;
4485 Name += "objc_msgSend_fixup";
4486 }
4487 }
4488 Name += '_';
4489 std::string SelName(Sel.getAsString());
4490 // Replace all ':' in selector name with '_' ouch!
4491 for(unsigned i = 0; i < SelName.size(); i++)
4492 if (SelName[i] == ':')
4493 SelName[i] = '_';
4494 Name += SelName;
4495 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4496 if (!GV) {
4497 // Build messafe ref table entry.
4498 std::vector<llvm::Constant*> Values(2);
4499 Values[0] = Fn;
4500 Values[1] = GetMethodVarName(Sel);
4501 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4502 GV = new llvm::GlobalVariable(Init->getType(), false,
4503 llvm::GlobalValue::WeakLinkage,
4504 Init,
4505 Name,
4506 &CGM.getModule());
4507 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4508 GV->setAlignment(
4509 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4510 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4511 UsedGlobals.push_back(GV);
4512 }
4513 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004514
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004515 CallArgList ActualArgs;
4516 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4517 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4518 ObjCTypes.MessageRefCPtrTy));
4519 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004520 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4521 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4522 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004523 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004524 Callee = CGF.Builder.CreateBitCast(Callee,
4525 llvm::PointerType::getUnqual(FTy));
4526 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004527}
4528
4529/// Generate code for a message send expression in the nonfragile abi.
4530CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4531 CodeGen::CodeGenFunction &CGF,
4532 QualType ResultType,
4533 Selector Sel,
4534 llvm::Value *Receiver,
4535 bool IsClassMessage,
4536 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004537 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004538 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004539 false, CallArgs);
4540}
4541
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004542llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004543 const ObjCInterfaceDecl *ID,
4544 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004545
4546 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4547
4548 if (!Entry) {
4549 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4550 llvm::GlobalVariable *ClassGV =
4551 CGM.getModule().getGlobalVariable(ClassName);
4552 if (!ClassGV) {
4553 ClassGV =
4554 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4555 llvm::GlobalValue::ExternalLinkage,
4556 0,
4557 ClassName,
4558 &CGM.getModule());
4559 UsedGlobals.push_back(ClassGV);
4560 }
4561 Entry =
4562 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4563 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004564 ClassGV,
4565 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4566 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004567 &CGM.getModule());
4568 Entry->setAlignment(
4569 CGM.getTargetData().getPrefTypeAlignment(
4570 ObjCTypes.ClassnfABIPtrTy));
4571
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004572 if (IsSuper)
Fariborz Jahanian21228b72009-02-26 22:30:39 +00004573 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004574 else
Fariborz Jahanian21228b72009-02-26 22:30:39 +00004575 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004576 UsedGlobals.push_back(Entry);
4577 }
4578
4579 return Builder.CreateLoad(Entry, false, "tmp");
4580}
4581
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004582/// EmitMetaClassRef - Return a Value * of the address of _class_t
4583/// meta-data
4584///
4585llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4586 const ObjCInterfaceDecl *ID) {
4587 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4588 if (Entry)
4589 return Builder.CreateLoad(Entry, false, "tmp");
4590
4591 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4592 llvm::GlobalVariable *MetaClassGV =
4593 CGM.getModule().getGlobalVariable(MetaClassName);
4594 if (!MetaClassGV) {
4595 MetaClassGV =
4596 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4597 llvm::GlobalValue::ExternalLinkage,
4598 0,
4599 MetaClassName,
4600 &CGM.getModule());
4601 UsedGlobals.push_back(MetaClassGV);
4602 }
4603
4604 Entry =
4605 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4606 llvm::GlobalValue::InternalLinkage,
4607 MetaClassGV,
4608 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4609 &CGM.getModule());
4610 Entry->setAlignment(
4611 CGM.getTargetData().getPrefTypeAlignment(
4612 ObjCTypes.ClassnfABIPtrTy));
4613
4614 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4615 UsedGlobals.push_back(Entry);
4616
4617 return Builder.CreateLoad(Entry, false, "tmp");
4618}
4619
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004620/// GetClass - Return a reference to the class for the given interface
4621/// decl.
4622llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4623 const ObjCInterfaceDecl *ID) {
4624 return EmitClassRef(Builder, ID);
4625}
4626
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004627/// Generates a message send where the super is the receiver. This is
4628/// a message send to self with special delivery semantics indicating
4629/// which class's method should be called.
4630CodeGen::RValue
4631CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4632 QualType ResultType,
4633 Selector Sel,
4634 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00004635 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004636 llvm::Value *Receiver,
4637 bool IsClassMessage,
4638 const CodeGen::CallArgList &CallArgs) {
4639 // ...
4640 // Create and init a super structure; this is a (receiver, class)
4641 // pair we will pass to objc_msgSendSuper.
4642 llvm::Value *ObjCSuper =
4643 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4644
4645 llvm::Value *ReceiverAsObject =
4646 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4647 CGF.Builder.CreateStore(ReceiverAsObject,
4648 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4649
4650 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00004651 llvm::Value *Target;
4652 if (IsClassMessage) {
4653 if (isCategoryImpl) {
4654 // Message sent to "super' in a class method defined in
4655 // a category implementation.
4656 Target = EmitClassRef(CGF.Builder, Class, false);
4657 Target = CGF.Builder.CreateStructGEP(Target, 0);
4658 Target = CGF.Builder.CreateLoad(Target);
4659 }
4660 else
4661 Target = EmitMetaClassRef(CGF.Builder, Class);
4662 }
4663 else
4664 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004665
4666 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4667 // and ObjCTypes types.
4668 const llvm::Type *ClassTy =
4669 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4670 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4671 CGF.Builder.CreateStore(Target,
4672 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4673
4674 return EmitMessageSend(CGF, ResultType, Sel,
4675 ObjCSuper, ObjCTypes.SuperPtrCTy,
4676 true, CallArgs);
4677}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004678
4679llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4680 Selector Sel) {
4681 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4682
4683 if (!Entry) {
4684 llvm::Constant *Casted =
4685 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4686 ObjCTypes.SelectorPtrTy);
4687 Entry =
4688 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4689 llvm::GlobalValue::InternalLinkage,
4690 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4691 &CGM.getModule());
4692 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4693 UsedGlobals.push_back(Entry);
4694 }
4695
4696 return Builder.CreateLoad(Entry, false, "tmp");
4697}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004698/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4699/// objc_assign_ivar (id src, id *dst)
4700///
4701void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4702 llvm::Value *src, llvm::Value *dst)
4703{
4704 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4705 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4706 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4707 src, dst, "assignivar");
4708 return;
4709}
4710
4711/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4712/// objc_assign_strongCast (id src, id *dst)
4713///
4714void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4715 CodeGen::CodeGenFunction &CGF,
4716 llvm::Value *src, llvm::Value *dst)
4717{
4718 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4719 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4720 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4721 src, dst, "weakassign");
4722 return;
4723}
4724
4725/// EmitObjCWeakRead - Code gen for loading value of a __weak
4726/// object: objc_read_weak (id *src)
4727///
4728llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4729 CodeGen::CodeGenFunction &CGF,
4730 llvm::Value *AddrWeakObj)
4731{
4732 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4733 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4734 AddrWeakObj, "weakread");
4735 return read_weak;
4736}
4737
4738/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4739/// objc_assign_weak (id src, id *dst)
4740///
4741void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4742 llvm::Value *src, llvm::Value *dst)
4743{
4744 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4745 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4746 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4747 src, dst, "weakassign");
4748 return;
4749}
4750
4751/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4752/// objc_assign_global (id src, id *dst)
4753///
4754void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4755 llvm::Value *src, llvm::Value *dst)
4756{
4757 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4758 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4759 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4760 src, dst, "globalassign");
4761 return;
4762}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004763
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004764void
4765CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
4766 const Stmt &S) {
4767 // We don't handle anything interesting yet.
4768 if (const ObjCAtTryStmt *TS = dyn_cast<ObjCAtTryStmt>(&S))
4769 if (TS->getCatchStmts())
4770 return CGF.ErrorUnsupported(&S, "try (with catch) statement");
4771
4772 bool isTry = isa<ObjCAtTryStmt>(S);
4773 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
4774 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
4775 llvm::BasicBlock *LandingPad = CGF.createBasicBlock("try.pad");
4776 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
4777 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
4778
4779 // For @synchronized, call objc_sync_enter(sync.expr). The
4780 // evaluation of the expression must occur before we enter the
4781 // @synchronized. We can safely avoid a temp here because jumps into
4782 // @synchronized are illegal & this will dominate uses.
4783 llvm::Value *SyncArg = 0;
4784 if (!isTry) {
4785 SyncArg =
4786 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
4787 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
4788 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
4789 }
4790
4791 // Push an EH context entry, used for handling rethrows and jumps
4792 // through finally.
4793 CGF.PushCleanupBlock(FinallyBlock);
4794
4795 CGF.setInvokeDest(LandingPad);
4796
4797 CGF.EmitBlock(TryBlock);
4798 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
4799 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
4800 CGF.EmitBranchThroughCleanup(FinallyEnd);
4801
4802 // Pop the cleanup entry, the @finally is outside this cleanup
4803 // scope.
4804 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
4805 CGF.setInvokeDest(PrevLandingPad);
4806
4807 CGF.EmitBlock(FinallyBlock);
4808
4809 if (isTry) {
4810 if (const ObjCAtFinallyStmt* FinallyStmt =
4811 cast<ObjCAtTryStmt>(S).getFinallyStmt())
4812 CGF.EmitStmt(FinallyStmt->getFinallyBody());
4813 } else {
4814 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
4815 // @synchronized.
4816 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
4817 }
4818
4819 if (Info.SwitchBlock)
4820 CGF.EmitBlock(Info.SwitchBlock);
4821 if (Info.EndBlock)
4822 CGF.EmitBlock(Info.EndBlock);
4823
4824 // Branch around the landing pad if necessary.
4825 CGF.EmitBranch(FinallyEnd);
4826
4827 // Emit the landing pad.
4828
4829 // Clear insertion point to avoid chaining.
4830 CGF.Builder.ClearInsertionPoint();
4831 CGF.EmitBlock(LandingPad);
4832
4833 llvm::Value *llvm_eh_exception =
4834 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
4835 llvm::Value *llvm_eh_selector_i64 =
4836 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
4837 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
4838
4839 llvm::SmallVector<llvm::Value*, 8> Args;
4840 Args.push_back(Exc);
4841 Args.push_back(ObjCTypes.EHPersonalityPtr);
4842 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
4843
4844 llvm::Value *Selector =
4845 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
4846
4847 // The only valid result for the limited case we are considering is
4848 // the cleanup.
4849 (void) Selector;
4850
4851 // Re-emit cleanup code for exceptional case.
4852 if (isTry) {
4853 // FIXME: This is horrible, in many ways: (a) it is broken because
4854 // we are messing with some global data structures (like where
4855 // labels point at), (b) it is exponential in the size of code
4856 // generated, (c) seriously, its just gross.
4857 if (const ObjCAtFinallyStmt* FinallyStmt =
4858 cast<ObjCAtTryStmt>(S).getFinallyStmt())
4859 CGF.EmitStmt(FinallyStmt->getFinallyBody());
4860 } else {
4861 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
4862 // @synchronized.
4863 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
4864 }
4865
4866 CGF.EnsureInsertPoint();
4867 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, Exc);
4868 CGF.Builder.CreateUnreachable();
4869
4870 CGF.EmitBlock(FinallyEnd);
4871}
4872
Anders Carlssonf57c5b22009-02-16 22:59:18 +00004873/// EmitThrowStmt - Generate code for a throw statement.
4874void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
4875 const ObjCAtThrowStmt &S) {
4876 llvm::Value *ExceptionAsObject;
4877
4878 if (const Expr *ThrowExpr = S.getThrowExpr()) {
4879 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
4880 ExceptionAsObject =
4881 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
4882
4883 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
4884 CGF.Builder.CreateUnreachable();
4885 } else {
4886 CGF.ErrorUnsupported(&S, "rethrow statement");
4887 }
4888
4889 // Clear the insertion point to indicate we are in unreachable code.
4890 CGF.Builder.ClearInsertionPoint();
4891}
4892
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004893/* *** */
4894
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004895CodeGen::CGObjCRuntime *
4896CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004897 return new CGObjCMac(CGM);
4898}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004899
4900CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004901CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004902 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004903}