blob: 2e059ad52e02a588c01c870cf8996a322f442a65 [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:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +000044 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000045 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
Chris Lattner74391b42009-03-22 21:03:39 +000087 llvm::Constant *GetPropertyFn, *SetPropertyFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000088
Chris Lattner74391b42009-03-22 21:03:39 +000089 llvm::Constant *EnumerationMutationFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000090
91 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner74391b42009-03-22 21:03:39 +000092 llvm::Constant *GcReadWeakFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000093
94 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner74391b42009-03-22 21:03:39 +000095 llvm::Constant *GcAssignWeakFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000096
97 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner74391b42009-03-22 21:03:39 +000098 llvm::Constant *GcAssignGlobalFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000099
100 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner74391b42009-03-22 21:03:39 +0000101 llvm::Constant *GcAssignIvarFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000102
103 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner74391b42009-03-22 21:03:39 +0000104 llvm::Constant *GcAssignStrongCastFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000105
106 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner74391b42009-03-22 21:03:39 +0000107 llvm::Constant *ExceptionThrowFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000108
Daniel Dunbar1c566672009-02-24 01:43:46 +0000109 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattner74391b42009-03-22 21:03:39 +0000110 llvm::Constant *SyncEnterFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000111
112 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000113 llvm::Constant *SyncExitFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000114
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
Chris Lattner74391b42009-03-22 21:03:39 +0000124 llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
125 llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn,
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000126 *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.
Chris Lattner74391b42009-03-22 21:03:39 +0000184 llvm::Constant *ExceptionTryEnterFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000185
186 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000187 llvm::Constant *ExceptionTryExitFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000188
189 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner74391b42009-03-22 21:03:39 +0000190 llvm::Constant *ExceptionExtractFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000191
192 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner74391b42009-03-22 21:03:39 +0000193 llvm::Constant *ExceptionMatchFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000194
195 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner74391b42009-03-22 21:03:39 +0000196 llvm::Constant *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
Chris Lattner74391b42009-03-22 21:03:39 +0000203 llvm::Constant *getSendFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000204 return IsSuper ? MessageSendSuperFn : MessageSendFn;
205 }
206
Chris Lattner74391b42009-03-22 21:03:39 +0000207 llvm::Constant *getSendStretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000208 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
209 }
210
Chris Lattner74391b42009-03-22 21:03:39 +0000211 llvm::Constant *getSendFpretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000212 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:
Chris Lattner74391b42009-03-22 21:03:39 +0000220 llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000221 *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
Chris Lattner74391b42009-03-22 21:03:39 +0000300 llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn;
Daniel Dunbare588b992009-03-01 04:46:24 +0000301
302 const llvm::StructType *EHTypeTy;
303 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000304
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000305 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
306 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000307};
308
309class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000310public:
311 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000312 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000313 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000314 unsigned int ivar_bytepos;
315 unsigned int ivar_size;
316 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
317 };
318
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000319 class SKIP_SCAN {
320 public:
321 unsigned int skip;
322 unsigned int scan;
323 SKIP_SCAN() : skip(0), scan(0) {}
324 };
325
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000326protected:
327 CodeGen::CodeGenModule &CGM;
328 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000329 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000330
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000331 // gc ivar layout bitmap calculation helper caches.
332 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
333 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
334 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000335
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000336 /// LazySymbols - Symbols to generate a lazy reference for. See
337 /// DefinedSymbols and FinishModule().
338 std::set<IdentifierInfo*> LazySymbols;
339
340 /// DefinedSymbols - External symbols which are defined by this
341 /// module. The symbols in this list and LazySymbols are used to add
342 /// special linker symbols which ensure that Objective-C modules are
343 /// linked properly.
344 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000345
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000346 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000347 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000348
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000349 /// MethodVarNames - uniqued method variable names.
350 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000351
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000352 /// MethodVarTypes - uniqued method type signatures. We have to use
353 /// a StringMap here because have no other unique reference.
354 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000355
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000356 /// MethodDefinitions - map of methods which have been defined in
357 /// this translation unit.
358 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000359
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000360 /// PropertyNames - uniqued method variable names.
361 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000362
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000363 /// ClassReferences - uniqued class references.
364 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000365
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000366 /// SelectorReferences - uniqued selector references.
367 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000368
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000369 /// Protocols - Protocols for which an objc_protocol structure has
370 /// been emitted. Forward declarations are handled by creating an
371 /// empty structure whose initializer is filled in when/if defined.
372 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000373
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000374 /// DefinedProtocols - Protocols which have actually been
375 /// defined. We should not need this, see FIXME in GenerateProtocol.
376 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000377
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000378 /// DefinedClasses - List of defined classes.
379 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000380
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000381 /// DefinedCategories - List of defined categories.
382 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000383
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000384 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000385 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000386 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000387
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000388 /// GetNameForMethod - Return a name for the given method.
389 /// \param[out] NameOut - The return value.
390 void GetNameForMethod(const ObjCMethodDecl *OMD,
391 const ObjCContainerDecl *CD,
392 std::string &NameOut);
393
394 /// GetMethodVarName - Return a unique constant for the given
395 /// selector's name. The return value has type char *.
396 llvm::Constant *GetMethodVarName(Selector Sel);
397 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
398 llvm::Constant *GetMethodVarName(const std::string &Name);
399
400 /// GetMethodVarType - Return a unique constant for the given
401 /// selector's name. The return value has type char *.
402
403 // FIXME: This is a horrible name.
404 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Devang Patel7794bb82009-03-04 18:21:39 +0000405 llvm::Constant *GetMethodVarType(FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000406
407 /// GetPropertyName - Return a unique constant for the given
408 /// name. The return value has type char *.
409 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
410
411 // FIXME: This can be dropped once string functions are unified.
412 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
413 const Decl *Container);
414
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000415 /// GetClassName - Return a unique constant for the given selector's
416 /// name. The return value has type char *.
417 llvm::Constant *GetClassName(IdentifierInfo *Ident);
418
Fariborz Jahanian21e6f172009-03-11 21:42:00 +0000419 /// GetInterfaceDeclStructLayout - Get layout for ivars of given
420 /// interface declaration.
421 const llvm::StructLayout *GetInterfaceDeclStructLayout(
422 const ObjCInterfaceDecl *ID) const;
423
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000424 /// BuildIvarLayout - Builds ivar layout bitmap for the class
425 /// implementation for the __strong or __weak case.
426 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000427 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
428 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000429
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000430 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
431 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000432 const RecordDecl *RD,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000433 const std::vector<FieldDecl*>& RecFields,
434 unsigned int BytePos, bool ForStrongLayout,
435 int &Index, int &SkIndex, bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000436
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000437 /// GetIvarLayoutName - Returns a unique constant for the given
438 /// ivar layout bitmap.
439 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
440 const ObjCCommonTypesHelper &ObjCTypes);
441
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000442 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
443 RecordDecl::field_iterator &FIV,
444 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000445 /// EmitPropertyList - Emit the given property list. The return
446 /// value has type PropertyListPtrTy.
447 llvm::Constant *EmitPropertyList(const std::string &Name,
448 const Decl *Container,
449 const ObjCContainerDecl *OCD,
450 const ObjCCommonTypesHelper &ObjCTypes);
451
Fariborz Jahanianda320092009-01-29 19:24:30 +0000452 /// GetProtocolRef - Return a reference to the internal protocol
453 /// description, creating an empty one if it has not been
454 /// defined. The return value has type ProtocolPtrTy.
455 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000456
457 /// GetIvarBaseOffset - returns ivars byte offset.
458 uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout,
459 FieldDecl *Field);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000460
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000461 /// GetFieldBaseOffset - return's field byt offset.
462 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
463 const llvm::StructLayout *Layout,
464 FieldDecl *Field);
465
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000466 /// CreateMetadataVar - Create a global variable with internal
467 /// linkage for use by the Objective-C runtime.
468 ///
469 /// This is a convenience wrapper which not only creates the
470 /// variable, but also sets the section and alignment and adds the
471 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000472 ///
473 /// \param Name - The variable name.
474 /// \param Init - The variable initializer; this is also used to
475 /// define the type of the variable.
476 /// \param Section - The section the variable should go into, or 0.
477 /// \param Align - The alignment for the variable, or 0.
478 /// \param AddToUsed - Whether the variable should be added to
479 /// llvm.
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000480 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
481 llvm::Constant *Init,
482 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000483 unsigned Align,
484 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000485
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000486public:
487 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
488 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000489
490 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000491
492 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
493 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000494
495 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
496
497 /// GetOrEmitProtocol - Get the protocol object for the given
498 /// declaration, emitting it if necessary. The return value has type
499 /// ProtocolPtrTy.
500 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
501
502 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
503 /// object for the given declaration, emitting it if needed. These
504 /// forward references will be filled in with empty bodies if no
505 /// definition is seen. The return value has type ProtocolPtrTy.
506 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000507};
508
509class CGObjCMac : public CGObjCCommonMac {
510private:
511 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000512 /// EmitImageInfo - Emit the image info marker used to encode some module
513 /// level information.
514 void EmitImageInfo();
515
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000516 /// EmitModuleInfo - Another marker encoding module level
517 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000518 void EmitModuleInfo();
519
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000520 /// EmitModuleSymols - Emit module symbols, the list of defined
521 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000522 llvm::Constant *EmitModuleSymbols();
523
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000524 /// FinishModule - Write out global data structures at the end of
525 /// processing a translation unit.
526 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000527
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 /// EmitClassExtension - Generate the class extension structure used
529 /// to store the weak ivar layout and properties. The return value
530 /// has type ClassExtensionPtrTy.
531 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
532
533 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
534 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000535 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000536 const ObjCInterfaceDecl *ID);
537
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000538 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000539 QualType ResultType,
540 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000541 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000542 QualType Arg0Ty,
543 bool IsSuper,
544 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000545
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000546 /// EmitIvarList - Emit the ivar list for the given
547 /// implementation. If ForClass is true the list of class ivars
548 /// (i.e. metaclass ivars) is emitted, otherwise the list of
549 /// interface ivars will be emitted. The return value has type
550 /// IvarListPtrTy.
551 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000552 bool ForClass);
553
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000554 /// EmitMetaClass - Emit a forward reference to the class structure
555 /// for the metaclass of the given interface. The return value has
556 /// type ClassPtrTy.
557 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
558
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000559 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000560 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000561 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
562 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000563 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000564 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000565
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000566 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000567
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000568 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000569
570 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000571 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000572 llvm::Constant *EmitMethodList(const std::string &Name,
573 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000574 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000575
576 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000577 /// method declarations.
578 /// - TypeName: The name for the type containing the methods.
579 /// - IsProtocol: True iff these methods are for a protocol.
580 /// - ClassMethds: True iff these are class methods.
581 /// - Required: When true, only "required" methods are
582 /// listed. Similarly, when false only "optional" methods are
583 /// listed. For classes this should always be true.
584 /// - begin, end: The method list to output.
585 ///
586 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000587 llvm::Constant *EmitMethodDescList(const std::string &Name,
588 const char *Section,
589 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000590
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000591 /// GetOrEmitProtocol - Get the protocol object for the given
592 /// declaration, emitting it if necessary. The return value has type
593 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000594 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000595
596 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
597 /// object for the given declaration, emitting it if needed. These
598 /// forward references will be filled in with empty bodies if no
599 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000600 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000601
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000602 /// EmitProtocolExtension - Generate the protocol extension
603 /// structure used to store optional instance and class methods, and
604 /// protocol properties. The return value has type
605 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000606 llvm::Constant *
607 EmitProtocolExtension(const ObjCProtocolDecl *PD,
608 const ConstantVector &OptInstanceMethods,
609 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000610
611 /// EmitProtocolList - Generate the list of referenced
612 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000613 llvm::Constant *EmitProtocolList(const std::string &Name,
614 ObjCProtocolDecl::protocol_iterator begin,
615 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000616
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000617 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
618 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000619 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000620
Fariborz Jahanianda320092009-01-29 19:24:30 +0000621 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000622 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000623
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000624 virtual llvm::Function *ModuleInitFunction();
625
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000626 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000627 QualType ResultType,
628 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000629 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000630 bool IsClassMessage,
631 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000632
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000633 virtual CodeGen::RValue
634 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000635 QualType ResultType,
636 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000637 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000638 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000639 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000640 bool IsClassMessage,
641 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000642
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000643 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000644 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000645
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000646 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000647
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000648 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000649
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000650 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000651
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000652 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000653 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000654
Chris Lattner74391b42009-03-22 21:03:39 +0000655 virtual llvm::Constant *GetPropertyGetFunction();
656 virtual llvm::Constant *GetPropertySetFunction();
657 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000658
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000659 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
660 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000661 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
662 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000663 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000664 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000665 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
666 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000667 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
668 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000669 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
670 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000671 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
672 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000673
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000674 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
675 QualType ObjectTy,
676 llvm::Value *BaseValue,
677 const ObjCIvarDecl *Ivar,
678 const FieldDecl *Field,
679 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000680 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
681 ObjCInterfaceDecl *Interface,
682 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000683};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000684
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000685class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000686private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000687 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000688 llvm::GlobalVariable* ObjCEmptyCacheVar;
689 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000690
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000691 /// MetaClassReferences - uniqued meta class references.
692 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +0000693
694 /// EHTypeReferences - uniqued class ehtype references.
695 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000696
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000697 /// FinishNonFragileABIModule - Write out global data structures at the end of
698 /// processing a translation unit.
699 void FinishNonFragileABIModule();
700
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000701 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
702 unsigned InstanceStart,
703 unsigned InstanceSize,
704 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000705 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
706 llvm::Constant *IsAGV,
707 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000708 llvm::Constant *ClassRoGV,
709 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000710
711 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
712
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000713 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
714
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000715 /// EmitMethodList - Emit the method list for the given
716 /// implementation. The return value has type MethodListnfABITy.
717 llvm::Constant *EmitMethodList(const std::string &Name,
718 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000719 const ConstantVector &Methods);
720 /// EmitIvarList - Emit the ivar list for the given
721 /// implementation. If ForClass is true the list of class ivars
722 /// (i.e. metaclass ivars) is emitted, otherwise the list of
723 /// interface ivars will be emitted. The return value has type
724 /// IvarListnfABIPtrTy.
725 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000726
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000727 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000728 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000729 unsigned long int offset);
730
Fariborz Jahanianda320092009-01-29 19:24:30 +0000731 /// GetOrEmitProtocol - Get the protocol object for the given
732 /// declaration, emitting it if necessary. The return value has type
733 /// ProtocolPtrTy.
734 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
735
736 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
737 /// object for the given declaration, emitting it if needed. These
738 /// forward references will be filled in with empty bodies if no
739 /// definition is seen. The return value has type ProtocolPtrTy.
740 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
741
742 /// EmitProtocolList - Generate the list of referenced
743 /// protocols. The return value has type ProtocolListPtrTy.
744 llvm::Constant *EmitProtocolList(const std::string &Name,
745 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000746 ObjCProtocolDecl::protocol_iterator end);
747
748 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
749 QualType ResultType,
750 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000751 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000752 QualType Arg0Ty,
753 bool IsSuper,
754 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +0000755
756 /// GetClassGlobal - Return the global variable for the Objective-C
757 /// class of the given name.
758 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000759
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000760 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
761 /// for the given class.
762 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000763 const ObjCInterfaceDecl *ID,
764 bool IsSuper = false);
765
766 /// EmitMetaClassRef - Return a Value * of the address of _class_t
767 /// meta-data
768 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
769 const ObjCInterfaceDecl *ID);
770
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000771 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
772 /// the given ivar.
773 ///
774 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000775 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000776 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000777
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000778 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
779 /// for the given selector.
780 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +0000781
782 /// GetInterfaceEHType - Get the ehtype for the given Objective-C
783 /// interface. The return value has type EHTypePtrTy.
784 llvm::Value *GetInterfaceEHType(const ObjCInterfaceType *IT);
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000785
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000786public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000787 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000788 // FIXME. All stubs for now!
789 virtual llvm::Function *ModuleInitFunction();
790
791 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
792 QualType ResultType,
793 Selector Sel,
794 llvm::Value *Receiver,
795 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000796 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000797
798 virtual CodeGen::RValue
799 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
800 QualType ResultType,
801 Selector Sel,
802 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000803 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000804 llvm::Value *Receiver,
805 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000806 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000807
808 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000809 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000810
811 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000812 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000813
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000814 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000815
816 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000817 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000818 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000819
Chris Lattner74391b42009-03-22 21:03:39 +0000820 virtual llvm::Constant *GetPropertyGetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000821 return ObjCTypes.GetPropertyFn;
822 }
Chris Lattner74391b42009-03-22 21:03:39 +0000823 virtual llvm::Constant *GetPropertySetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000824 return ObjCTypes.SetPropertyFn;
825 }
Chris Lattner74391b42009-03-22 21:03:39 +0000826 virtual llvm::Constant *EnumerationMutationFunction() {
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000827 return ObjCTypes.EnumerationMutationFn;
828 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000829
830 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000831 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000832 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000833 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000834 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000835 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000836 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000837 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000838 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000839 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000840 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000841 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000842 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000843 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000844 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
845 QualType ObjectTy,
846 llvm::Value *BaseValue,
847 const ObjCIvarDecl *Ivar,
848 const FieldDecl *Field,
849 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000850 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
851 ObjCInterfaceDecl *Interface,
852 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000853};
854
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000855} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000856
857/* *** Helper Functions *** */
858
859/// getConstantGEP() - Help routine to construct simple GEPs.
860static llvm::Constant *getConstantGEP(llvm::Constant *C,
861 unsigned idx0,
862 unsigned idx1) {
863 llvm::Value *Idxs[] = {
864 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
865 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
866 };
867 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
868}
869
870/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000871
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000872CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
873 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000874{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000875 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000876 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000877}
878
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000879/// GetClass - Return a reference to the class for the given interface
880/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000881llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 const ObjCInterfaceDecl *ID) {
883 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000884}
885
886/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000887llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000888 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000889}
890
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000891/// Generate a constant CFString object.
892/*
893 struct __builtin_CFString {
894 const int *isa; // point to __CFConstantStringClassReference
895 int flags;
896 const char *str;
897 long length;
898 };
899*/
900
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000901llvm::Constant *CGObjCCommonMac::GenerateConstantString(
902 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000903 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000904}
905
906/// Generates a message send where the super is the receiver. This is
907/// a message send to self with special delivery semantics indicating
908/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000909CodeGen::RValue
910CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000911 QualType ResultType,
912 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000913 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000914 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000915 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000916 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000917 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000918 // Create and init a super structure; this is a (receiver, class)
919 // pair we will pass to objc_msgSendSuper.
920 llvm::Value *ObjCSuper =
921 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
922 llvm::Value *ReceiverAsObject =
923 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
924 CGF.Builder.CreateStore(ReceiverAsObject,
925 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000926
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000927 // If this is a class message the metaclass is passed as the target.
928 llvm::Value *Target;
929 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000930 if (isCategoryImpl) {
931 // Message sent to 'super' in a class method defined in a category
932 // implementation requires an odd treatment.
933 // If we are in a class method, we must retrieve the
934 // _metaclass_ for the current class, pointed at by
935 // the class's "isa" pointer. The following assumes that
936 // isa" is the first ivar in a class (which it must be).
937 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
938 Target = CGF.Builder.CreateStructGEP(Target, 0);
939 Target = CGF.Builder.CreateLoad(Target);
940 }
941 else {
942 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
943 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
944 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
945 Target = Super;
946 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000947 } else {
948 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
949 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000950 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
951 // and ObjCTypes types.
952 const llvm::Type *ClassTy =
953 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000954 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000955 CGF.Builder.CreateStore(Target,
956 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
957
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000958 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000959 ObjCSuper, ObjCTypes.SuperPtrCTy,
960 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000961}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000962
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000963/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000964CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000965 QualType ResultType,
966 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000967 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000968 bool IsClassMessage,
969 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000970 llvm::Value *Arg0 =
971 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000972 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000973 Arg0, CGF.getContext().getObjCIdType(),
974 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000975}
976
977CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000978 QualType ResultType,
979 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000980 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000981 QualType Arg0Ty,
982 bool IsSuper,
983 const CallArgList &CallArgs) {
984 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000985 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
986 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
987 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000988 CGF.getContext().getObjCSelType()));
989 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000990
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000991 CodeGenTypes &Types = CGM.getTypes();
992 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
993 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000994
995 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000996 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000997 Fn = ObjCTypes.getSendStretFn(IsSuper);
998 } else if (ResultType->isFloatingType()) {
999 // FIXME: Sadly, this is wrong. This actually depends on the
1000 // architecture. This happens to be right for x86-32 though.
1001 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1002 } else {
1003 Fn = ObjCTypes.getSendFn(IsSuper);
1004 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001005 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001006 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001007}
1008
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001009llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001010 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001011 // FIXME: I don't understand why gcc generates this, or where it is
1012 // resolved. Investigate. Its also wasteful to look this up over and
1013 // over.
1014 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1015
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1017 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018}
1019
Fariborz Jahanianda320092009-01-29 19:24:30 +00001020void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001021 // FIXME: We shouldn't need this, the protocol decl should contain
1022 // enough information to tell us whether this was a declaration or a
1023 // definition.
1024 DefinedProtocols.insert(PD->getIdentifier());
1025
1026 // If we have generated a forward reference to this protocol, emit
1027 // it now. Otherwise do nothing, the protocol objects are lazily
1028 // emitted.
1029 if (Protocols.count(PD->getIdentifier()))
1030 GetOrEmitProtocol(PD);
1031}
1032
Fariborz Jahanianda320092009-01-29 19:24:30 +00001033llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001034 if (DefinedProtocols.count(PD->getIdentifier()))
1035 return GetOrEmitProtocol(PD);
1036 return GetOrEmitProtocolRef(PD);
1037}
1038
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039/*
1040 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1041 struct _objc_protocol {
1042 struct _objc_protocol_extension *isa;
1043 char *protocol_name;
1044 struct _objc_protocol_list *protocol_list;
1045 struct _objc__method_prototype_list *instance_methods;
1046 struct _objc__method_prototype_list *class_methods
1047 };
1048
1049 See EmitProtocolExtension().
1050*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001051llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1052 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1053
1054 // Early exit if a defining object has already been generated.
1055 if (Entry && Entry->hasInitializer())
1056 return Entry;
1057
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001058 // FIXME: I don't understand why gcc generates this, or where it is
1059 // resolved. Investigate. Its also wasteful to look this up over and
1060 // over.
1061 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1062
Chris Lattner8ec03f52008-11-24 03:54:41 +00001063 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064
1065 // Construct method lists.
1066 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1067 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
1068 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
1069 e = PD->instmeth_end(); i != e; ++i) {
1070 ObjCMethodDecl *MD = *i;
1071 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1072 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1073 OptInstanceMethods.push_back(C);
1074 } else {
1075 InstanceMethods.push_back(C);
1076 }
1077 }
1078
1079 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
1080 e = PD->classmeth_end(); i != e; ++i) {
1081 ObjCMethodDecl *MD = *i;
1082 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1083 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1084 OptClassMethods.push_back(C);
1085 } else {
1086 ClassMethods.push_back(C);
1087 }
1088 }
1089
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001090 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001091 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001093 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001094 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001095 PD->protocol_begin(),
1096 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001097 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001098 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1099 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1101 InstanceMethods);
1102 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001103 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1104 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001105 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1106 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1108 Values);
1109
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001110 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001111 // Already created, fix the linkage and update the initializer.
1112 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001113 Entry->setInitializer(Init);
1114 } else {
1115 Entry =
1116 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1117 llvm::GlobalValue::InternalLinkage,
1118 Init,
1119 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1120 &CGM.getModule());
1121 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001122 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001123 UsedGlobals.push_back(Entry);
1124 // FIXME: Is this necessary? Why only for protocol?
1125 Entry->setAlignment(4);
1126 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001127
1128 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001129}
1130
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001131llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001132 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1133
1134 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001135 // We use the initializer as a marker of whether this is a forward
1136 // reference or not. At module finalization we add the empty
1137 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138 Entry =
1139 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001140 llvm::GlobalValue::ExternalLinkage,
1141 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001142 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001143 &CGM.getModule());
1144 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001145 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146 UsedGlobals.push_back(Entry);
1147 // FIXME: Is this necessary? Why only for protocol?
1148 Entry->setAlignment(4);
1149 }
1150
1151 return Entry;
1152}
1153
1154/*
1155 struct _objc_protocol_extension {
1156 uint32_t size;
1157 struct objc_method_description_list *optional_instance_methods;
1158 struct objc_method_description_list *optional_class_methods;
1159 struct objc_property_list *instance_properties;
1160 };
1161*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001162llvm::Constant *
1163CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1164 const ConstantVector &OptInstanceMethods,
1165 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001166 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001167 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001168 std::vector<llvm::Constant*> Values(4);
1169 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001170 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001171 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1172 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001173 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1174 OptInstanceMethods);
1175 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001176 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1177 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001178 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1179 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001180 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1181 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001182 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001183
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001184 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001185 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1186 Values[3]->isNullValue())
1187 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1188
1189 llvm::Constant *Init =
1190 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001192 // No special section, but goes in llvm.used
1193 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1194 Init,
1195 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001196}
1197
1198/*
1199 struct objc_protocol_list {
1200 struct objc_protocol_list *next;
1201 long count;
1202 Protocol *list[];
1203 };
1204*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001205llvm::Constant *
1206CGObjCMac::EmitProtocolList(const std::string &Name,
1207 ObjCProtocolDecl::protocol_iterator begin,
1208 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001209 std::vector<llvm::Constant*> ProtocolRefs;
1210
Daniel Dunbardbc933702008-08-21 21:57:41 +00001211 for (; begin != end; ++begin)
1212 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001213
1214 // Just return null for empty protocol lists
1215 if (ProtocolRefs.empty())
1216 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1217
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001218 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001219 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1220
1221 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001222 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001223 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1224 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1225 Values[2] =
1226 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1227 ProtocolRefs.size()),
1228 ProtocolRefs);
1229
1230 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1231 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001232 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001233 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001234 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1235}
1236
1237/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001238 struct _objc_property {
1239 const char * const name;
1240 const char * const attributes;
1241 };
1242
1243 struct _objc_property_list {
1244 uint32_t entsize; // sizeof (struct _objc_property)
1245 uint32_t prop_count;
1246 struct _objc_property[prop_count];
1247 };
1248*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001249llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1250 const Decl *Container,
1251 const ObjCContainerDecl *OCD,
1252 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001253 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001254 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1255 E = OCD->prop_end(); I != E; ++I) {
1256 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001257 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001258 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001259 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1260 Prop));
1261 }
1262
1263 // Return null for empty list.
1264 if (Properties.empty())
1265 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1266
1267 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001268 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001269 std::vector<llvm::Constant*> Values(3);
1270 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1271 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1272 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1273 Properties.size());
1274 Values[2] = llvm::ConstantArray::get(AT, Properties);
1275 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1276
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001277 // No special section on property lists?
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001278 llvm::GlobalVariable *GV =
1279 CreateMetadataVar(Name, Init, (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
1280 0, true);
1281 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001282}
1283
1284/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001285 struct objc_method_description_list {
1286 int count;
1287 struct objc_method_description list[];
1288 };
1289*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001290llvm::Constant *
1291CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1292 std::vector<llvm::Constant*> Desc(2);
1293 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1294 ObjCTypes.SelectorPtrTy);
1295 Desc[1] = GetMethodVarType(MD);
1296 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1297 Desc);
1298}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001299
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001300llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1301 const char *Section,
1302 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001303 // Return null for empty list.
1304 if (Methods.empty())
1305 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1306
1307 std::vector<llvm::Constant*> Values(2);
1308 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1309 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1310 Methods.size());
1311 Values[1] = llvm::ConstantArray::get(AT, Methods);
1312 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1313
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001314 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001315 return llvm::ConstantExpr::getBitCast(GV,
1316 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001317}
1318
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001319/*
1320 struct _objc_category {
1321 char *category_name;
1322 char *class_name;
1323 struct _objc_method_list *instance_methods;
1324 struct _objc_method_list *class_methods;
1325 struct _objc_protocol_list *protocols;
1326 uint32_t size; // <rdar://4585769>
1327 struct _objc_property_list *instance_properties;
1328 };
1329 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001330void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001331 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001332
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001333 // FIXME: This is poor design, the OCD should have a pointer to the
1334 // category decl. Additionally, note that Category can be null for
1335 // the @implementation w/o an @interface case. Sema should just
1336 // create one for us as it does for @implementation so everyone else
1337 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001338 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001339 const ObjCCategoryDecl *Category =
1340 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001341 std::string ExtName(Interface->getNameAsString() + "_" +
1342 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001343
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001344 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1345 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1346 e = OCD->instmeth_end(); i != e; ++i) {
1347 // Instance methods should always be defined.
1348 InstanceMethods.push_back(GetMethodConstant(*i));
1349 }
1350 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1351 e = OCD->classmeth_end(); i != e; ++i) {
1352 // Class methods should always be defined.
1353 ClassMethods.push_back(GetMethodConstant(*i));
1354 }
1355
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001356 std::vector<llvm::Constant*> Values(7);
1357 Values[0] = GetClassName(OCD->getIdentifier());
1358 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001359 Values[2] =
1360 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1361 ExtName,
1362 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001363 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001364 Values[3] =
1365 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1366 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001367 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001368 if (Category) {
1369 Values[4] =
1370 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1371 Category->protocol_begin(),
1372 Category->protocol_end());
1373 } else {
1374 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1375 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001376 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001377
1378 // If there is no category @interface then there can be no properties.
1379 if (Category) {
1380 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001381 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001382 } else {
1383 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1384 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001385
1386 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1387 Values);
1388
1389 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001390 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1391 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001392 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001393 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001394}
1395
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001396// FIXME: Get from somewhere?
1397enum ClassFlags {
1398 eClassFlags_Factory = 0x00001,
1399 eClassFlags_Meta = 0x00002,
1400 // <rdr://5142207>
1401 eClassFlags_HasCXXStructors = 0x02000,
1402 eClassFlags_Hidden = 0x20000,
1403 eClassFlags_ABI2_Hidden = 0x00010,
1404 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1405};
1406
1407// <rdr://5142207&4705298&4843145>
1408static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1409 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1410 // FIXME: Support -fvisibility
1411 switch (attr->getVisibility()) {
1412 default:
1413 assert(0 && "Unknown visibility");
1414 return false;
1415 case VisibilityAttr::DefaultVisibility:
1416 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1417 return false;
1418 case VisibilityAttr::HiddenVisibility:
1419 return true;
1420 }
1421 } else {
1422 return false; // FIXME: Support -fvisibility
1423 }
1424}
1425
1426/*
1427 struct _objc_class {
1428 Class isa;
1429 Class super_class;
1430 const char *name;
1431 long version;
1432 long info;
1433 long instance_size;
1434 struct _objc_ivar_list *ivars;
1435 struct _objc_method_list *methods;
1436 struct _objc_cache *cache;
1437 struct _objc_protocol_list *protocols;
1438 // Objective-C 1.0 extensions (<rdr://4585769>)
1439 const char *ivar_layout;
1440 struct _objc_class_ext *ext;
1441 };
1442
1443 See EmitClassExtension();
1444 */
1445void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001446 DefinedSymbols.insert(ID->getIdentifier());
1447
Chris Lattner8ec03f52008-11-24 03:54:41 +00001448 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001449 // FIXME: Gross
1450 ObjCInterfaceDecl *Interface =
1451 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001452 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001453 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001454 Interface->protocol_begin(),
1455 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001456 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001457 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001458 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001459 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001460
1461 // FIXME: Set CXX-structors flag.
1462 if (IsClassHidden(ID->getClassInterface()))
1463 Flags |= eClassFlags_Hidden;
1464
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001465 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1466 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1467 e = ID->instmeth_end(); i != e; ++i) {
1468 // Instance methods should always be defined.
1469 InstanceMethods.push_back(GetMethodConstant(*i));
1470 }
1471 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1472 e = ID->classmeth_end(); i != e; ++i) {
1473 // Class methods should always be defined.
1474 ClassMethods.push_back(GetMethodConstant(*i));
1475 }
1476
1477 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1478 e = ID->propimpl_end(); i != e; ++i) {
1479 ObjCPropertyImplDecl *PID = *i;
1480
1481 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1482 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1483
1484 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1485 if (llvm::Constant *C = GetMethodConstant(MD))
1486 InstanceMethods.push_back(C);
1487 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1488 if (llvm::Constant *C = GetMethodConstant(MD))
1489 InstanceMethods.push_back(C);
1490 }
1491 }
1492
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001493 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001494 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001496 // Record a reference to the super class.
1497 LazySymbols.insert(Super->getIdentifier());
1498
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001499 Values[ 1] =
1500 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1501 ObjCTypes.ClassPtrTy);
1502 } else {
1503 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1504 }
1505 Values[ 2] = GetClassName(ID->getIdentifier());
1506 // Version is always 0.
1507 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1508 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1509 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001510 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001511 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001512 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001513 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001514 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001515 // cache is always NULL.
1516 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1517 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001518 // FIXME: Set ivar_layout
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001519 Values[10] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001520 Values[11] = EmitClassExtension(ID);
1521 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1522 Values);
1523
1524 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001525 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1526 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001527 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001528 DefinedClasses.push_back(GV);
1529}
1530
1531llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1532 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001533 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001534 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001535 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001536 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001537
1538 if (IsClassHidden(ID->getClassInterface()))
1539 Flags |= eClassFlags_Hidden;
1540
1541 std::vector<llvm::Constant*> Values(12);
1542 // The isa for the metaclass is the root of the hierarchy.
1543 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1544 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1545 Root = Super;
1546 Values[ 0] =
1547 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1548 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001549 // The super class for the metaclass is emitted as the name of the
1550 // super class. The runtime fixes this up to point to the
1551 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001552 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1553 Values[ 1] =
1554 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1555 ObjCTypes.ClassPtrTy);
1556 } else {
1557 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1558 }
1559 Values[ 2] = GetClassName(ID->getIdentifier());
1560 // Version is always 0.
1561 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1562 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1563 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001564 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001565 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001566 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001567 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001568 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001569 // cache is always NULL.
1570 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1571 Values[ 9] = Protocols;
1572 // ivar_layout for metaclass is always NULL.
1573 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1574 // The class extension is always unused for metaclasses.
1575 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1576 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1577 Values);
1578
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001579 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001580 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001581
1582 // Check for a forward reference.
1583 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1584 if (GV) {
1585 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1586 "Forward metaclass reference has incorrect type.");
1587 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1588 GV->setInitializer(Init);
1589 } else {
1590 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1591 llvm::GlobalValue::InternalLinkage,
1592 Init, Name,
1593 &CGM.getModule());
1594 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001595 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001596 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001597 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001598
1599 return GV;
1600}
1601
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001602llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001603 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001604
1605 // FIXME: Should we look these up somewhere other than the
1606 // module. Its a bit silly since we only generate these while
1607 // processing an implementation, so exactly one pointer would work
1608 // if know when we entered/exitted an implementation block.
1609
1610 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001611 // Previously, metaclass with internal linkage may have been defined.
1612 // pass 'true' as 2nd argument so it is returned.
1613 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001614 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1615 "Forward metaclass reference has incorrect type.");
1616 return GV;
1617 } else {
1618 // Generate as an external reference to keep a consistent
1619 // module. This will be patched up when we emit the metaclass.
1620 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1621 llvm::GlobalValue::ExternalLinkage,
1622 0,
1623 Name,
1624 &CGM.getModule());
1625 }
1626}
1627
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001628/*
1629 struct objc_class_ext {
1630 uint32_t size;
1631 const char *weak_ivar_layout;
1632 struct _objc_property_list *properties;
1633 };
1634*/
1635llvm::Constant *
1636CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1637 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001638 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001639
1640 std::vector<llvm::Constant*> Values(3);
1641 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001642 // FIXME: Output weak_ivar_layout string.
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001643 Values[1] = GetIvarLayoutName(0, ObjCTypes);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001644 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001645 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001646
1647 // Return null if no extension bits are used.
1648 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1649 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1650
1651 llvm::Constant *Init =
1652 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001653 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
1654 Init, 0, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001655}
1656
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001657/// countInheritedIvars - count number of ivars in class and its super class(s)
1658///
1659static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1660 int count = 0;
1661 if (!OI)
1662 return 0;
1663 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1664 if (SuperClass)
1665 count += countInheritedIvars(SuperClass);
1666 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1667 E = OI->ivar_end(); I != E; ++I)
1668 ++count;
1669 return count;
1670}
1671
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001672/// getInterfaceDeclForIvar - Get the interface declaration node where
1673/// this ivar is declared in.
1674/// FIXME. Ideally, this info should be in the ivar node. But currently
1675/// it is not and prevailing wisdom is that ASTs should not have more
1676/// info than is absolutely needed, even though this info reflects the
1677/// source language.
1678///
1679static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1680 const ObjCInterfaceDecl *OI,
1681 const ObjCIvarDecl *IVD) {
1682 if (!OI)
1683 return 0;
1684 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1685 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1686 E = OI->ivar_end(); I != E; ++I)
1687 if ((*I)->getIdentifier() == IVD->getIdentifier())
1688 return OI;
1689 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1690}
1691
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001692/*
1693 struct objc_ivar {
1694 char *ivar_name;
1695 char *ivar_type;
1696 int ivar_offset;
1697 };
1698
1699 struct objc_ivar_list {
1700 int ivar_count;
1701 struct objc_ivar list[count];
1702 };
1703 */
1704llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001705 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001706 std::vector<llvm::Constant*> Ivars, Ivar(3);
1707
1708 // When emitting the root class GCC emits ivar entries for the
1709 // actual class structure. It is not clear if we need to follow this
1710 // behavior; for now lets try and get away with not doing it. If so,
1711 // the cleanest solution would be to make up an ObjCInterfaceDecl
1712 // for the class.
1713 if (ForClass)
1714 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001715
1716 ObjCInterfaceDecl *OID =
1717 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00001718 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001719
1720 RecordDecl::field_iterator ifield, pfield;
1721 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001722 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1723 FieldDecl *Field = *ifield;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001724 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001725 if (Field->getIdentifier())
1726 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1727 else
1728 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00001729 Ivar[1] = GetMethodVarType(Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001730 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001731 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001732 }
1733
1734 // Return null for empty list.
1735 if (Ivars.empty())
1736 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1737
1738 std::vector<llvm::Constant*> Values(2);
1739 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1740 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1741 Ivars.size());
1742 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1743 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1744
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001745 llvm::GlobalVariable *GV;
1746 if (ForClass)
1747 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00001748 Init, "__OBJC,__class_vars,regular,no_dead_strip",
1749 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001750 else
1751 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
1752 + ID->getNameAsString(),
1753 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
1754 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001755 return llvm::ConstantExpr::getBitCast(GV,
1756 ObjCTypes.IvarListPtrTy);
1757}
1758
1759/*
1760 struct objc_method {
1761 SEL method_name;
1762 char *method_types;
1763 void *method;
1764 };
1765
1766 struct objc_method_list {
1767 struct objc_method_list *obsolete;
1768 int count;
1769 struct objc_method methods_list[count];
1770 };
1771*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001772
1773/// GetMethodConstant - Return a struct objc_method constant for the
1774/// given method if it has been defined. The result is null if the
1775/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001776llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001777 // FIXME: Use DenseMap::lookup
1778 llvm::Function *Fn = MethodDefinitions[MD];
1779 if (!Fn)
1780 return 0;
1781
1782 std::vector<llvm::Constant*> Method(3);
1783 Method[0] =
1784 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1785 ObjCTypes.SelectorPtrTy);
1786 Method[1] = GetMethodVarType(MD);
1787 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1788 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1789}
1790
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001791llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1792 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001793 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001794 // Return null for empty list.
1795 if (Methods.empty())
1796 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1797
1798 std::vector<llvm::Constant*> Values(3);
1799 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1800 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1801 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1802 Methods.size());
1803 Values[2] = llvm::ConstantArray::get(AT, Methods);
1804 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1805
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001806 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001807 return llvm::ConstantExpr::getBitCast(GV,
1808 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001809}
1810
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001811llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001812 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001813 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001814 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001815
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001816 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001817 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001818 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001819 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001820 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001821 llvm::GlobalValue::InternalLinkage,
1822 Name,
1823 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001824 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001825
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001826 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001827}
1828
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001829uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
1830 FieldDecl *Field) {
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00001831 if (!Field->isBitField())
1832 return Layout->getElementOffset(
1833 CGM.getTypes().getLLVMFieldNo(Field));
1834 // FIXME. Must be a better way of getting a bitfield base offset.
1835 uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field);
1836 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
1837 uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
1838 offset = (offset*size)/8;
1839 return offset;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001840}
1841
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001842/// GetFieldBaseOffset - return's field byt offset.
1843uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
1844 const llvm::StructLayout *Layout,
1845 FieldDecl *Field) {
1846 const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1847 Field = const_cast<ObjCInterfaceDecl*>(OI)->lookupFieldDeclForIvar(
1848 CGM.getContext(), Ivar);
1849 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
1850 return Offset;
1851}
1852
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001853llvm::GlobalVariable *
1854CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
1855 llvm::Constant *Init,
1856 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001857 unsigned Align,
1858 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001859 const llvm::Type *Ty = Init->getType();
1860 llvm::GlobalVariable *GV =
1861 new llvm::GlobalVariable(Ty, false,
1862 llvm::GlobalValue::InternalLinkage,
1863 Init,
1864 Name,
1865 &CGM.getModule());
1866 if (Section)
1867 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001868 if (Align)
1869 GV->setAlignment(Align);
1870 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001871 UsedGlobals.push_back(GV);
1872 return GV;
1873}
1874
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001875llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001876 // Abuse this interface function as a place to finalize.
1877 FinishModule();
1878
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001879 return NULL;
1880}
1881
Chris Lattner74391b42009-03-22 21:03:39 +00001882llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001883 return ObjCTypes.GetPropertyFn;
1884}
1885
Chris Lattner74391b42009-03-22 21:03:39 +00001886llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001887 return ObjCTypes.SetPropertyFn;
1888}
1889
Chris Lattner74391b42009-03-22 21:03:39 +00001890llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001891 return ObjCTypes.EnumerationMutationFn;
1892}
1893
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001894/*
1895
1896Objective-C setjmp-longjmp (sjlj) Exception Handling
1897--
1898
1899The basic framework for a @try-catch-finally is as follows:
1900{
1901 objc_exception_data d;
1902 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001903 bool _call_try_exit = true;
1904
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001905 objc_exception_try_enter(&d);
1906 if (!setjmp(d.jmp_buf)) {
1907 ... try body ...
1908 } else {
1909 // exception path
1910 id _caught = objc_exception_extract(&d);
1911
1912 // enter new try scope for handlers
1913 if (!setjmp(d.jmp_buf)) {
1914 ... match exception and execute catch blocks ...
1915
1916 // fell off end, rethrow.
1917 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001918 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001919 } else {
1920 // exception in catch block
1921 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001922 _call_try_exit = false;
1923 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001924 }
1925 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001926 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001927
1928finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001929 if (_call_try_exit)
1930 objc_exception_try_exit(&d);
1931
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001932 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001933 ... dispatch to finally destination ...
1934
1935finally_rethrow:
1936 objc_exception_throw(_rethrow);
1937
1938finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001939}
1940
1941This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001942uses _rethrow to determine if objc_exception_try_exit should be called
1943and if the object should be rethrown. This breaks in the face of
1944throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001945
1946We specialize this framework for a few particular circumstances:
1947
1948 - If there are no catch blocks, then we avoid emitting the second
1949 exception handling context.
1950
1951 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1952 e)) we avoid emitting the code to rethrow an uncaught exception.
1953
1954 - FIXME: If there is no @finally block we can do a few more
1955 simplifications.
1956
1957Rethrows and Jumps-Through-Finally
1958--
1959
1960Support for implicit rethrows and jumping through the finally block is
1961handled by storing the current exception-handling context in
1962ObjCEHStack.
1963
Daniel Dunbar898d5082008-09-30 01:06:03 +00001964In order to implement proper @finally semantics, we support one basic
1965mechanism for jumping through the finally block to an arbitrary
1966destination. Constructs which generate exits from a @try or @catch
1967block use this mechanism to implement the proper semantics by chaining
1968jumps, as necessary.
1969
1970This mechanism works like the one used for indirect goto: we
1971arbitrarily assign an ID to each destination and store the ID for the
1972destination in a variable prior to entering the finally block. At the
1973end of the finally block we simply create a switch to the proper
1974destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001975
1976Code gen for @synchronized(expr) stmt;
1977Effectively generating code for:
1978objc_sync_enter(expr);
1979@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001980*/
1981
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001982void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1983 const Stmt &S) {
1984 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001985 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001986 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001987 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001988 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1989 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1990 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00001991
1992 // For @synchronized, call objc_sync_enter(sync.expr). The
1993 // evaluation of the expression must occur before we enter the
1994 // @synchronized. We can safely avoid a temp here because jumps into
1995 // @synchronized are illegal & this will dominate uses.
1996 llvm::Value *SyncArg = 0;
1997 if (!isTry) {
1998 SyncArg =
1999 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2000 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
2001 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
2002 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002003
2004 // Push an EH context entry, used for handling rethrows and jumps
2005 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002006 CGF.PushCleanupBlock(FinallyBlock);
2007
Anders Carlsson273558f2009-02-07 21:37:21 +00002008 CGF.ObjCEHValueStack.push_back(0);
2009
Daniel Dunbar898d5082008-09-30 01:06:03 +00002010 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002011 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2012 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002013 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2014 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002015 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2016 "_call_try_exit");
2017 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2018
Anders Carlsson80f25672008-09-09 17:59:25 +00002019 // Enter a new try block and call setjmp.
2020 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
2021 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2022 "jmpbufarray");
2023 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
2024 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2025 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002026
Daniel Dunbar55e87422008-11-11 02:29:29 +00002027 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2028 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002029 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002030 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002031
2032 // Emit the @try block.
2033 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002034 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2035 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002036 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002037
2038 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002039 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002040
2041 // Retrieve the exception object. We may emit multiple blocks but
2042 // nothing can cross this so the value is already in SSA form.
2043 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2044 ExceptionData,
2045 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002046 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002047 if (!isTry)
2048 {
2049 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002050 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002051 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002052 }
2053 else if (const ObjCAtCatchStmt* CatchStmt =
2054 cast<ObjCAtTryStmt>(S).getCatchStmts())
2055 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002056 // Enter a new exception try block (in case a @catch block throws
2057 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00002058 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002059
Anders Carlsson80f25672008-09-09 17:59:25 +00002060 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2061 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002062 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002063
Daniel Dunbar55e87422008-11-11 02:29:29 +00002064 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2065 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002066 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002067
2068 CGF.EmitBlock(CatchBlock);
2069
Daniel Dunbar55e40722008-09-27 07:03:52 +00002070 // Handle catch list. As a special case we check if everything is
2071 // matched and avoid generating code for falling off the end if
2072 // so.
2073 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002074 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002075 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002076
Steve Naroff7ba138a2009-03-03 19:52:17 +00002077 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002078 const PointerType *PT = 0;
2079
Anders Carlsson80f25672008-09-09 17:59:25 +00002080 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002081 if (!CatchParam) {
2082 AllMatched = true;
2083 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002084 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002085
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002086 // catch(id e) always matches.
2087 // FIXME: For the time being we also match id<X>; this should
2088 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002089 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002090 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002091 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002092 }
2093
Daniel Dunbar55e40722008-09-27 07:03:52 +00002094 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002095 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002096 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002097 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002098 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002099 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002100
Anders Carlssondde0a942008-09-11 09:15:33 +00002101 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002102 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002103 break;
2104 }
2105
Daniel Dunbar129271a2008-09-27 07:36:24 +00002106 assert(PT && "Unexpected non-pointer type in @catch");
2107 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002108 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002109 assert(ObjCType && "Catch parameter must have Objective-C type!");
2110
2111 // Check if the @catch block matches the exception object.
2112 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2113
Anders Carlsson80f25672008-09-09 17:59:25 +00002114 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2115 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002116
Daniel Dunbar55e87422008-11-11 02:29:29 +00002117 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002118
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002119 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002120 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002121
2122 // Emit the @catch block.
2123 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002124 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002125 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002126
2127 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002128 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002129 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002130 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002131
2132 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002133 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002134
2135 CGF.EmitBlock(NextCatchBlock);
2136 }
2137
Daniel Dunbar55e40722008-09-27 07:03:52 +00002138 if (!AllMatched) {
2139 // None of the handlers caught the exception, so store it to be
2140 // rethrown at the end of the @finally block.
2141 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002142 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002143 }
2144
2145 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002146 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002147 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2148 ExceptionData),
2149 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002150 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002151 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002152 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002153 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002154 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002155 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002156 }
2157
Daniel Dunbar898d5082008-09-30 01:06:03 +00002158 // Pop the exception-handling stack entry. It is important to do
2159 // this now, because the code in the @finally block is not in this
2160 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002161 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2162
Anders Carlsson273558f2009-02-07 21:37:21 +00002163 CGF.ObjCEHValueStack.pop_back();
2164
Anders Carlsson80f25672008-09-09 17:59:25 +00002165 // Emit the @finally block.
2166 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002167 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2168
2169 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2170
2171 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002172 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002173
2174 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002175 if (isTry) {
2176 if (const ObjCAtFinallyStmt* FinallyStmt =
2177 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2178 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002179 } else {
2180 // Emit objc_sync_exit(expr); as finally's sole statement for
2181 // @synchronized.
2182 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002183 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002184
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002185 // Emit the switch block
2186 if (Info.SwitchBlock)
2187 CGF.EmitBlock(Info.SwitchBlock);
2188 if (Info.EndBlock)
2189 CGF.EmitBlock(Info.EndBlock);
2190
Daniel Dunbar898d5082008-09-30 01:06:03 +00002191 CGF.EmitBlock(FinallyRethrow);
2192 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2193 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002194 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002195
2196 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002197}
2198
2199void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002200 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002201 llvm::Value *ExceptionAsObject;
2202
2203 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2204 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2205 ExceptionAsObject =
2206 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2207 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002208 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002209 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002210 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002211 }
2212
2213 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002214 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002215
2216 // Clear the insertion point to indicate we are in unreachable code.
2217 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002218}
2219
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002220/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002221/// object: objc_read_weak (id *src)
2222///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002223llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002224 llvm::Value *AddrWeakObj)
2225{
Eli Friedman8339b352009-03-07 03:57:15 +00002226 const llvm::Type* DestTy =
2227 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002228 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002229 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002230 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002231 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002232 return read_weak;
2233}
2234
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002235/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2236/// objc_assign_weak (id src, id *dst)
2237///
2238void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2239 llvm::Value *src, llvm::Value *dst)
2240{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002241 const llvm::Type * SrcTy = src->getType();
2242 if (!isa<llvm::PointerType>(SrcTy)) {
2243 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2244 assert(Size <= 8 && "does not support size > 8");
2245 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2246 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002247 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2248 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002249 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2250 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002251 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2252 src, dst, "weakassign");
2253 return;
2254}
2255
Fariborz Jahanian58626502008-11-19 00:59:10 +00002256/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2257/// objc_assign_global (id src, id *dst)
2258///
2259void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2260 llvm::Value *src, llvm::Value *dst)
2261{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002262 const llvm::Type * SrcTy = src->getType();
2263 if (!isa<llvm::PointerType>(SrcTy)) {
2264 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2265 assert(Size <= 8 && "does not support size > 8");
2266 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2267 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002268 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2269 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002270 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2271 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002272 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2273 src, dst, "globalassign");
2274 return;
2275}
2276
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002277/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2278/// objc_assign_ivar (id src, id *dst)
2279///
2280void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2281 llvm::Value *src, llvm::Value *dst)
2282{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002283 const llvm::Type * SrcTy = src->getType();
2284 if (!isa<llvm::PointerType>(SrcTy)) {
2285 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2286 assert(Size <= 8 && "does not support size > 8");
2287 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2288 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002289 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2290 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002291 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2292 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2293 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2294 src, dst, "assignivar");
2295 return;
2296}
2297
Fariborz Jahanian58626502008-11-19 00:59:10 +00002298/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2299/// objc_assign_strongCast (id src, id *dst)
2300///
2301void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2302 llvm::Value *src, llvm::Value *dst)
2303{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002304 const llvm::Type * SrcTy = src->getType();
2305 if (!isa<llvm::PointerType>(SrcTy)) {
2306 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2307 assert(Size <= 8 && "does not support size > 8");
2308 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2309 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002310 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2311 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002312 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2313 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002314 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2315 src, dst, "weakassign");
2316 return;
2317}
2318
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002319/// EmitObjCValueForIvar - Code Gen for ivar reference.
2320///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002321LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2322 QualType ObjectTy,
2323 llvm::Value *BaseValue,
2324 const ObjCIvarDecl *Ivar,
2325 const FieldDecl *Field,
2326 unsigned CVRQualifiers) {
2327 if (Ivar->isBitField())
2328 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2329 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002330 // TODO: Add a special case for isa (index 0)
2331 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2332 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002333 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002334 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2335 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002336 LValue::SetObjCIvar(LV, true);
2337 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002338}
2339
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002340llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2341 ObjCInterfaceDecl *Interface,
2342 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002343 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002344 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00002345 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002346 return llvm::ConstantInt::get(
2347 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2348 Offset);
2349}
2350
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002351/* *** Private Interface *** */
2352
2353/// EmitImageInfo - Emit the image info marker used to encode some module
2354/// level information.
2355///
2356/// See: <rdr://4810609&4810587&4810587>
2357/// struct IMAGE_INFO {
2358/// unsigned version;
2359/// unsigned flags;
2360/// };
2361enum ImageInfoFlags {
2362 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2363 eImageInfo_GarbageCollected = (1 << 1),
2364 eImageInfo_GCOnly = (1 << 2)
2365};
2366
2367void CGObjCMac::EmitImageInfo() {
2368 unsigned version = 0; // Version is unused?
2369 unsigned flags = 0;
2370
2371 // FIXME: Fix and continue?
2372 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2373 flags |= eImageInfo_GarbageCollected;
2374 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2375 flags |= eImageInfo_GCOnly;
2376
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002377 // Emitted as int[2];
2378 llvm::Constant *values[2] = {
2379 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2380 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2381 };
2382 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002383
2384 const char *Section;
2385 if (ObjCABI == 1)
2386 Section = "__OBJC, __image_info,regular";
2387 else
2388 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002389 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002390 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2391 llvm::ConstantArray::get(AT, values, 2),
2392 Section,
2393 0,
2394 true);
2395 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002396}
2397
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002398
2399// struct objc_module {
2400// unsigned long version;
2401// unsigned long size;
2402// const char *name;
2403// Symtab symtab;
2404// };
2405
2406// FIXME: Get from somewhere
2407static const int ModuleVersion = 7;
2408
2409void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002410 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002411
2412 std::vector<llvm::Constant*> Values(4);
2413 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2414 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002415 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002416 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002417 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002418 CreateMetadataVar("\01L_OBJC_MODULES",
2419 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2420 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002421 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002422}
2423
2424llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002425 unsigned NumClasses = DefinedClasses.size();
2426 unsigned NumCategories = DefinedCategories.size();
2427
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002428 // Return null if no symbols were defined.
2429 if (!NumClasses && !NumCategories)
2430 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2431
2432 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2434 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2435 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2436 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2437
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002438 // The runtime expects exactly the list of defined classes followed
2439 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002441 for (unsigned i=0; i<NumClasses; i++)
2442 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2443 ObjCTypes.Int8PtrTy);
2444 for (unsigned i=0; i<NumCategories; i++)
2445 Symbols[NumClasses + i] =
2446 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2447 ObjCTypes.Int8PtrTy);
2448
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002449 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002450 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 NumClasses + NumCategories),
2452 Symbols);
2453
2454 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2455
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002456 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002457 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2458 "__OBJC,__symbols,regular,no_dead_strip",
2459 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002460 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2461}
2462
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002463llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002464 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002465 LazySymbols.insert(ID->getIdentifier());
2466
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002467 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2468
2469 if (!Entry) {
2470 llvm::Constant *Casted =
2471 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2472 ObjCTypes.ClassPtrTy);
2473 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002474 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2475 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
2476 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002477 }
2478
2479 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002480}
2481
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002482llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002483 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2484
2485 if (!Entry) {
2486 llvm::Constant *Casted =
2487 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2488 ObjCTypes.SelectorPtrTy);
2489 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002490 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2491 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
2492 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002493 }
2494
2495 return Builder.CreateLoad(Entry, false, "tmp");
2496}
2497
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002498llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002499 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002500
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002501 if (!Entry)
2502 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2503 llvm::ConstantArray::get(Ident->getName()),
2504 "__TEXT,__cstring,cstring_literals",
2505 0, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002506
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002507 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002508}
2509
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002510/// GetInterfaceDeclStructLayout - Get layout for ivars of given
2511/// interface declaration.
2512const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout(
2513 const ObjCInterfaceDecl *OID) const {
2514 const llvm::Type *InterfaceTy =
2515 CGM.getTypes().ConvertType(
2516 CGM.getContext().getObjCInterfaceType(
2517 const_cast<ObjCInterfaceDecl*>(OID)));
2518 const llvm::StructLayout *Layout =
2519 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
2520 return Layout;
2521}
2522
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002523/// GetIvarLayoutName - Returns a unique constant for the given
2524/// ivar layout bitmap.
2525llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2526 const ObjCCommonTypesHelper &ObjCTypes) {
2527 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2528}
2529
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002530void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2531 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002532 const RecordDecl *RD,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002533 const std::vector<FieldDecl*>& RecFields,
2534 unsigned int BytePos, bool ForStrongLayout,
2535 int &Index, int &SkIndex, bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002536 bool IsUnion = (RD && RD->isUnion());
2537 uint64_t MaxUnionIvarSize = 0;
2538 uint64_t MaxSkippedUnionIvarSize = 0;
2539 FieldDecl *MaxField = 0;
2540 FieldDecl *MaxSkippedField = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002541 unsigned int base = 0;
2542 if (RecFields.empty())
2543 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002544 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002545 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
2546 unsigned WordSizeInBits = CGM.getContext().getTypeSize(
2547 CGM.getContext().VoidPtrTy);
2548 unsigned ByteSizeInBits = CGM.getContext().getTypeSize(
2549 CGM.getContext().CharTy);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002550 for (unsigned i = 0; i < RecFields.size(); i++) {
2551 FieldDecl *Field = RecFields[i];
2552 // Skip over unnamed or bitfields
2553 if (!Field->getIdentifier() || Field->isBitField())
2554 continue;
2555 QualType FQT = Field->getType();
2556 if (FQT->isAggregateType()) {
2557 std::vector<FieldDecl*> NestedRecFields;
2558 if (FQT->isUnionType())
2559 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002560 else
2561 assert(FQT->isRecordType() &&
2562 "only union/record is supported for ivar layout bitmap");
2563
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002564 const RecordType *RT = FQT->getAsRecordType();
2565 const RecordDecl *RD = RT->getDecl();
2566 // FIXME - Find a more efficiant way of passing records down.
2567 unsigned j = 0;
2568 for (RecordDecl::field_iterator i = RD->field_begin(),
2569 e = RD->field_end(); i != e; ++i)
2570 NestedRecFields[j++] = (*i);
2571 // FIXME - Is Layout correct?
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002572 BuildAggrIvarLayout(OI, Layout, RD, NestedRecFields,
2573 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002574 ForStrongLayout, Index, SkIndex,
2575 HasUnion);
2576 continue;
2577 }
2578 else if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002579 const ConstantArrayType *CArray =
2580 dyn_cast_or_null<ConstantArrayType>(Array);
2581 assert(CArray && "only array with know element size is supported");
2582 FQT = CArray->getElementType();
2583 assert(!FQT->isUnionType() &&
2584 "layout for array of unions not supported");
2585 if (FQT->isRecordType()) {
2586 uint64_t ElCount = CArray->getSize().getZExtValue();
2587 int OldIndex = Index;
2588 int OldSkIndex = SkIndex;
2589
2590 std::vector<FieldDecl*> ElementRecFields;
2591 // FIXME - Use a common routine with the above!
2592 const RecordType *RT = FQT->getAsRecordType();
2593 const RecordDecl *RD = RT->getDecl();
2594 // FIXME - Find a more efficiant way of passing records down.
2595 unsigned j = 0;
2596 for (RecordDecl::field_iterator i = RD->field_begin(),
2597 e = RD->field_end(); i != e; ++i)
2598 ElementRecFields[j++] = (*i);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002599 BuildAggrIvarLayout(OI, Layout, RD,
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002600 ElementRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002601 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002602 ForStrongLayout, Index, SkIndex,
2603 HasUnion);
2604 // Replicate layout information for each array element. Note that
2605 // one element is already done.
2606 uint64_t ElIx = 1;
2607 for (int FirstIndex = Index, FirstSkIndex = SkIndex;
2608 ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002609 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002610 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2611 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002612 GC_IVAR gcivar;
2613 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
2614 gcivar.ivar_size = IvarsInfo[i].ivar_size;
2615 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002616 }
2617
2618 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
2619 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002620 GC_IVAR skivar;
2621 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
2622 skivar.ivar_size = SkipIvars[i].ivar_size;
2623 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002624 }
2625 }
2626 continue;
2627 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002628 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002629 // At this point, we are done with Record/Union and array there of.
2630 // For other arrays we are down to its element type.
2631 QualType::GCAttrTypes GCAttr = QualType::GCNone;
2632 do {
2633 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
2634 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
2635 break;
2636 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002637 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002638 GCAttr = QualType::Strong;
2639 break;
2640 }
2641 else if (const PointerType *PT = FQT->getAsPointerType()) {
2642 FQT = PT->getPointeeType();
2643 }
2644 else {
2645 break;
2646 }
2647 } while (true);
2648 if ((ForStrongLayout && GCAttr == QualType::Strong)
2649 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
2650 if (IsUnion)
2651 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002652 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
2653 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002654 if (UnionIvarSize > MaxUnionIvarSize)
2655 {
2656 MaxUnionIvarSize = UnionIvarSize;
2657 MaxField = Field;
2658 }
2659 }
2660 else
2661 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002662 GC_IVAR gcivar;
2663 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2664 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2665 WordSizeInBits;
2666 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002667 }
2668 }
2669 else if ((ForStrongLayout &&
2670 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
2671 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
2672 if (IsUnion)
2673 {
2674 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
2675 if (UnionIvarSize > MaxSkippedUnionIvarSize)
2676 {
2677 MaxSkippedUnionIvarSize = UnionIvarSize;
2678 MaxSkippedField = Field;
2679 }
2680 }
2681 else
2682 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002683 GC_IVAR skivar;
2684 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2685 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2686 WordSizeInBits;
2687 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002688 }
2689 }
2690 }
2691 if (MaxField)
2692 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002693 GC_IVAR gcivar;
2694 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
2695 gcivar.ivar_size = MaxUnionIvarSize;
2696 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002697 }
2698 if (MaxSkippedField)
2699 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002700 GC_IVAR skivar;
2701 skivar.ivar_bytepos = BytePos +
2702 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
2703 skivar.ivar_size = MaxSkippedUnionIvarSize;
2704 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002705 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002706 return;
2707}
2708
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002709static int
2710IvarBytePosCompare (const void *a, const void *b)
2711{
2712 unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos;
2713 unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos;
2714
2715 if (sa < sb)
2716 return -1;
2717 if (sa > sb)
2718 return 1;
2719 return 0;
2720}
2721
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002722/// BuildIvarLayout - Builds ivar layout bitmap for the class
2723/// implementation for the __strong or __weak case.
2724/// The layout map displays which words in ivar list must be skipped
2725/// and which must be scanned by GC (see below). String is built of bytes.
2726/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
2727/// of words to skip and right nibble is count of words to scan. So, each
2728/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
2729/// represented by a 0x00 byte which also ends the string.
2730/// 1. when ForStrongLayout is true, following ivars are scanned:
2731/// - id, Class
2732/// - object *
2733/// - __strong anything
2734///
2735/// 2. When ForStrongLayout is false, following ivars are scanned:
2736/// - __weak anything
2737///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002738llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002739 const ObjCImplementationDecl *OMD,
2740 bool ForStrongLayout) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002741 int Index = -1;
2742 int SkIndex = -1;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002743 bool hasUnion = false;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002744 int SkipScan;
2745 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002746 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2747 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
2748 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002749
2750 std::vector<FieldDecl*> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002751 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002752 CGM.getContext().CollectObjCIvars(OI, RecFields);
2753 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002754 return llvm::Constant::getNullValue(PtrTy);
2755 SkipIvars.clear();
2756 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002757
2758 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002759 BuildAggrIvarLayout (OI, Layout, 0, RecFields, 0, ForStrongLayout,
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002760 Index, SkIndex, hasUnion);
2761 if (Index == -1)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002762 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002763
2764 // Sort on byte position in case we encounterred a union nested in
2765 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002766 if (hasUnion && !IvarsInfo.empty())
2767 qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2768 if (hasUnion && !SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002769 qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2770
2771 // Build the string of skip/scan nibbles
2772 SkipScan = -1;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002773 SkipScanIvars.clear();
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002774 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002775 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002776 if (IvarsInfo[0].ivar_bytepos == 0) {
2777 WordsToSkip = 0;
2778 WordsToScan = IvarsInfo[0].ivar_size;
2779 }
2780 else {
2781 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
2782 WordsToScan = IvarsInfo[0].ivar_size;
2783 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002784 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002785 {
2786 unsigned int TailPrevGCObjC =
2787 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
2788 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
2789 {
2790 // consecutive 'scanned' object pointers.
2791 WordsToScan += IvarsInfo[i].ivar_size;
2792 }
2793 else
2794 {
2795 // Skip over 'gc'able object pointer which lay over each other.
2796 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
2797 continue;
2798 // Must skip over 1 or more words. We save current skip/scan values
2799 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002800 SKIP_SCAN SkScan;
2801 SkScan.skip = WordsToSkip;
2802 SkScan.scan = WordsToScan;
2803 SkipScanIvars.push_back(SkScan); ++SkipScan;
2804
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002805 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002806 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
2807 SkScan.scan = 0;
2808 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002809 WordsToSkip = 0;
2810 WordsToScan = IvarsInfo[i].ivar_size;
2811 }
2812 }
2813 if (WordsToScan > 0)
2814 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002815 SKIP_SCAN SkScan;
2816 SkScan.skip = WordsToSkip;
2817 SkScan.scan = WordsToScan;
2818 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002819 }
2820
2821 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002822 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002823 {
2824 int LastByteSkipped =
2825 SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size;
2826 int LastByteScanned =
2827 IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize;
2828 BytesSkipped = (LastByteSkipped > LastByteScanned);
2829 // Compute number of bytes to skip at the tail end of the last ivar scanned.
2830 if (BytesSkipped)
2831 {
2832 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002833 SKIP_SCAN SkScan;
2834 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
2835 SkScan.scan = 0;
2836 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002837 }
2838 }
2839 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
2840 // as 0xMN.
2841 for (int i = 0; i <= SkipScan; i++)
2842 {
2843 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
2844 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
2845 // 0xM0 followed by 0x0N detected.
2846 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
2847 for (int j = i+1; j < SkipScan; j++)
2848 SkipScanIvars[j] = SkipScanIvars[j+1];
2849 --SkipScan;
2850 }
2851 }
2852
2853 // Generate the string.
2854 std::string BitMap;
2855 for (int i = 0; i <= SkipScan; i++)
2856 {
2857 unsigned char byte;
2858 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
2859 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
2860 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
2861 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
2862
2863 if (skip_small > 0 || skip_big > 0)
2864 BytesSkipped = true;
2865 // first skip big.
2866 for (unsigned int ix = 0; ix < skip_big; ix++)
2867 BitMap += (unsigned char)(0xf0);
2868
2869 // next (skip small, scan)
2870 if (skip_small)
2871 {
2872 byte = skip_small << 4;
2873 if (scan_big > 0)
2874 {
2875 byte |= 0xf;
2876 --scan_big;
2877 }
2878 else if (scan_small)
2879 {
2880 byte |= scan_small;
2881 scan_small = 0;
2882 }
2883 BitMap += byte;
2884 }
2885 // next scan big
2886 for (unsigned int ix = 0; ix < scan_big; ix++)
2887 BitMap += (unsigned char)(0x0f);
2888 // last scan small
2889 if (scan_small)
2890 {
2891 byte = scan_small;
2892 BitMap += byte;
2893 }
2894 }
2895 // null terminate string.
2896 // BitMap += (unsigned char)0;
2897 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
2898 // final layout.
2899 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002900 return llvm::Constant::getNullValue(PtrTy);
2901 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2902 llvm::ConstantArray::get(BitMap.c_str()),
2903 "__TEXT,__cstring,cstring_literals",
2904 0, true);
2905 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002906}
2907
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002908llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002909 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2910
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002911 // FIXME: Avoid std::string copying.
2912 if (!Entry)
2913 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
2914 llvm::ConstantArray::get(Sel.getAsString()),
2915 "__TEXT,__cstring,cstring_literals",
2916 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002917
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002918 return getConstantGEP(Entry, 0, 0);
2919}
2920
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002921// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002922llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002923 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2924}
2925
2926// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002927llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002928 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2929}
2930
Devang Patel7794bb82009-03-04 18:21:39 +00002931llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) {
2932 std::string TypeStr;
2933 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
2934
2935 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002936
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002937 if (!Entry)
2938 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
2939 llvm::ConstantArray::get(TypeStr),
2940 "__TEXT,__cstring,cstring_literals",
2941 0, true);
2942
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002943 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002944}
2945
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002946llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002947 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002948 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2949 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00002950
2951 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
2952
2953 if (!Entry) {
2954 llvm::Constant *C = llvm::ConstantArray::get(TypeStr);
2955 Entry =
2956 new llvm::GlobalVariable(C->getType(), false,
2957 llvm::GlobalValue::InternalLinkage,
2958 C, "\01L_OBJC_METH_VAR_TYPE_",
2959 &CGM.getModule());
2960 Entry->setSection("__TEXT,__cstring,cstring_literals");
2961 UsedGlobals.push_back(Entry);
2962 }
2963
2964 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002965}
2966
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002967// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002968llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002969 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2970
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002971 if (!Entry)
2972 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
2973 llvm::ConstantArray::get(Ident->getName()),
2974 "__TEXT,__cstring,cstring_literals",
2975 0, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002976
2977 return getConstantGEP(Entry, 0, 0);
2978}
2979
2980// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002981// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002982llvm::Constant *
2983 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
2984 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002985 std::string TypeStr;
2986 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002987 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2988}
2989
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002990void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2991 const ObjCContainerDecl *CD,
2992 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002993 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002994 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002995 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002996 assert (CD && "Missing container decl in GetNameForMethod");
2997 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002998 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2999 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00003000 NameOut += ' ';
3001 NameOut += D->getSelector().getAsString();
3002 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003003}
3004
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003005/// GetFirstIvarInRecord - This routine returns the record for the
3006/// implementation of the fiven class OID. It also returns field
3007/// corresponding to the first ivar in the class in FIV. It also
3008/// returns the one before the first ivar.
3009///
3010const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
3011 const ObjCInterfaceDecl *OID,
3012 RecordDecl::field_iterator &FIV,
3013 RecordDecl::field_iterator &PIV) {
3014 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
3015 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
3016 RecordDecl::field_iterator ifield = RD->field_begin();
3017 RecordDecl::field_iterator pfield = RD->field_end();
3018 while (countSuperClassIvars-- > 0) {
3019 pfield = ifield;
3020 ++ifield;
3021 }
3022 FIV = ifield;
3023 PIV = pfield;
3024 return RD;
3025}
3026
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003027void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003028 EmitModuleInfo();
3029
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003030 // Emit the dummy bodies for any protocols which were referenced but
3031 // never defined.
3032 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3033 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3034 if (i->second->hasInitializer())
3035 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003036
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003037 std::vector<llvm::Constant*> Values(5);
3038 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3039 Values[1] = GetClassName(i->first);
3040 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3041 Values[3] = Values[4] =
3042 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3043 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3044 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3045 Values));
3046 }
3047
3048 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003049 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003050 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003051 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003052 }
3053
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003054 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003055 llvm::GlobalValue *GV =
3056 new llvm::GlobalVariable(AT, false,
3057 llvm::GlobalValue::AppendingLinkage,
3058 llvm::ConstantArray::get(AT, Used),
3059 "llvm.used",
3060 &CGM.getModule());
3061
3062 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003063
3064 // Add assembler directives to add lazy undefined symbol references
3065 // for classes which are referenced but not defined. This is
3066 // important for correct linker interaction.
3067
3068 // FIXME: Uh, this isn't particularly portable.
3069 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003070
3071 if (!CGM.getModule().getModuleInlineAsm().empty())
3072 s << "\n";
3073
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003074 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3075 e = LazySymbols.end(); i != e; ++i) {
3076 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3077 }
3078 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3079 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003080 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003081 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3082 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003083
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003084 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003085}
3086
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003087CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003088 : CGObjCCommonMac(cgm),
3089 ObjCTypes(cgm)
3090{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003091 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003092 ObjCABI = 2;
3093}
3094
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003095/* *** */
3096
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003097ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3098: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003099{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003100 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3101 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003102
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003103 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003104 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003105 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003106 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003107 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3108
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003109 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003110 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003111 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003112
3113 // FIXME: It would be nice to unify this with the opaque type, so
3114 // that the IR comes out a bit cleaner.
3115 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3116 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003117
3118 // I'm not sure I like this. The implicit coordination is a bit
3119 // gross. We should solve this in a reasonable fashion because this
3120 // is a pretty common task (match some runtime data structure with
3121 // an LLVM data structure).
3122
3123 // FIXME: This is leaked.
3124 // FIXME: Merge with rewriter code?
3125
3126 // struct _objc_super {
3127 // id self;
3128 // Class cls;
3129 // }
3130 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3131 SourceLocation(),
3132 &Ctx.Idents.get("_objc_super"));
3133 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3134 Ctx.getObjCIdType(), 0, false));
3135 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3136 Ctx.getObjCClassType(), 0, false));
3137 RD->completeDefinition(Ctx);
3138
3139 SuperCTy = Ctx.getTagDeclType(RD);
3140 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3141
3142 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003143 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3144
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003145 // struct _prop_t {
3146 // char *name;
3147 // char *attributes;
3148 // }
3149 PropertyTy = llvm::StructType::get(Int8PtrTy,
3150 Int8PtrTy,
3151 NULL);
3152 CGM.getModule().addTypeName("struct._prop_t",
3153 PropertyTy);
3154
3155 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003156 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003157 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003158 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003159 // }
3160 PropertyListTy = llvm::StructType::get(IntTy,
3161 IntTy,
3162 llvm::ArrayType::get(PropertyTy, 0),
3163 NULL);
3164 CGM.getModule().addTypeName("struct._prop_list_t",
3165 PropertyListTy);
3166 // struct _prop_list_t *
3167 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3168
3169 // struct _objc_method {
3170 // SEL _cmd;
3171 // char *method_type;
3172 // char *_imp;
3173 // }
3174 MethodTy = llvm::StructType::get(SelectorPtrTy,
3175 Int8PtrTy,
3176 Int8PtrTy,
3177 NULL);
3178 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003179
3180 // struct _objc_cache *
3181 CacheTy = llvm::OpaqueType::get();
3182 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3183 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003184
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003185 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003186
3187 QualType IdType = Ctx.getObjCIdType();
3188 QualType SelType = Ctx.getObjCSelType();
3189 llvm::SmallVector<QualType,16> Params;
3190 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003191
3192 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003193 Params.push_back(IdType);
3194 Params.push_back(SelType);
3195 Params.push_back(Ctx.LongTy);
3196 Params.push_back(Ctx.BoolTy);
3197 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
3198 false);
3199 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003200
3201 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
3202 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003203 Params.push_back(IdType);
3204 Params.push_back(SelType);
3205 Params.push_back(Ctx.LongTy);
3206 Params.push_back(IdType);
3207 Params.push_back(Ctx.BoolTy);
3208 Params.push_back(Ctx.BoolTy);
3209 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3210 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
3211
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003212 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003213
3214 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003215 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003216 Params.push_back(IdType);
3217 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3218 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
3219 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003220
3221 // gc's API
3222 // id objc_read_weak (id *)
3223 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003224 Params.push_back(Ctx.getPointerType(IdType));
3225 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3226 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
3227
3228 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003229 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003230 Params.push_back(IdType);
3231 Params.push_back(Ctx.getPointerType(IdType));
3232
3233 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3234 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
3235 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
3236 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
3237 GcAssignStrongCastFn =
3238 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003239
3240 // void objc_exception_throw(id)
3241 Params.clear();
3242 Params.push_back(IdType);
3243
3244 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003245 ExceptionThrowFn =
3246 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00003247
3248 // synchronized APIs
3249 // void objc_sync_enter (id)
3250 // void objc_sync_exit (id)
3251 Params.clear();
3252 Params.push_back(IdType);
3253
3254 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3255 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
3256 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003257}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003258
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003259ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3260 : ObjCCommonTypesHelper(cgm)
3261{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003262 // struct _objc_method_description {
3263 // SEL name;
3264 // char *types;
3265 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003266 MethodDescriptionTy =
3267 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003268 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003269 NULL);
3270 CGM.getModule().addTypeName("struct._objc_method_description",
3271 MethodDescriptionTy);
3272
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003273 // struct _objc_method_description_list {
3274 // int count;
3275 // struct _objc_method_description[1];
3276 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003277 MethodDescriptionListTy =
3278 llvm::StructType::get(IntTy,
3279 llvm::ArrayType::get(MethodDescriptionTy, 0),
3280 NULL);
3281 CGM.getModule().addTypeName("struct._objc_method_description_list",
3282 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003283
3284 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003285 MethodDescriptionListPtrTy =
3286 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3287
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003288 // Protocol description structures
3289
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003290 // struct _objc_protocol_extension {
3291 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3292 // struct _objc_method_description_list *optional_instance_methods;
3293 // struct _objc_method_description_list *optional_class_methods;
3294 // struct _objc_property_list *instance_properties;
3295 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003296 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003297 llvm::StructType::get(IntTy,
3298 MethodDescriptionListPtrTy,
3299 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003300 PropertyListPtrTy,
3301 NULL);
3302 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3303 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003304
3305 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003306 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3307
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003308 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003309
3310 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3311 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3312
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003313 const llvm::Type *T =
3314 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3315 LongTy,
3316 llvm::ArrayType::get(ProtocolTyHolder, 0),
3317 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003318 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3319
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003320 // struct _objc_protocol {
3321 // struct _objc_protocol_extension *isa;
3322 // char *protocol_name;
3323 // struct _objc_protocol **_objc_protocol_list;
3324 // struct _objc_method_description_list *instance_methods;
3325 // struct _objc_method_description_list *class_methods;
3326 // }
3327 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003328 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003329 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3330 MethodDescriptionListPtrTy,
3331 MethodDescriptionListPtrTy,
3332 NULL);
3333 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3334
3335 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3336 CGM.getModule().addTypeName("struct._objc_protocol_list",
3337 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003338 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003339 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3340
3341 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003342 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003343 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003344
3345 // Class description structures
3346
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003347 // struct _objc_ivar {
3348 // char *ivar_name;
3349 // char *ivar_type;
3350 // int ivar_offset;
3351 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003352 IvarTy = llvm::StructType::get(Int8PtrTy,
3353 Int8PtrTy,
3354 IntTy,
3355 NULL);
3356 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3357
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003358 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003359 IvarListTy = llvm::OpaqueType::get();
3360 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3361 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3362
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003363 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003364 MethodListTy = llvm::OpaqueType::get();
3365 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3366 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3367
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003368 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003369 ClassExtensionTy =
3370 llvm::StructType::get(IntTy,
3371 Int8PtrTy,
3372 PropertyListPtrTy,
3373 NULL);
3374 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3375 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3376
3377 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3378
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003379 // struct _objc_class {
3380 // Class isa;
3381 // Class super_class;
3382 // char *name;
3383 // long version;
3384 // long info;
3385 // long instance_size;
3386 // struct _objc_ivar_list *ivars;
3387 // struct _objc_method_list *methods;
3388 // struct _objc_cache *cache;
3389 // struct _objc_protocol_list *protocols;
3390 // char *ivar_layout;
3391 // struct _objc_class_ext *ext;
3392 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003393 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3394 llvm::PointerType::getUnqual(ClassTyHolder),
3395 Int8PtrTy,
3396 LongTy,
3397 LongTy,
3398 LongTy,
3399 IvarListPtrTy,
3400 MethodListPtrTy,
3401 CachePtrTy,
3402 ProtocolListPtrTy,
3403 Int8PtrTy,
3404 ClassExtensionPtrTy,
3405 NULL);
3406 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3407
3408 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3409 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3410 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3411
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003412 // struct _objc_category {
3413 // char *category_name;
3414 // char *class_name;
3415 // struct _objc_method_list *instance_method;
3416 // struct _objc_method_list *class_method;
3417 // uint32_t size; // sizeof(struct _objc_category)
3418 // struct _objc_property_list *instance_properties;// category's @property
3419 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003420 CategoryTy = llvm::StructType::get(Int8PtrTy,
3421 Int8PtrTy,
3422 MethodListPtrTy,
3423 MethodListPtrTy,
3424 ProtocolListPtrTy,
3425 IntTy,
3426 PropertyListPtrTy,
3427 NULL);
3428 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3429
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003430 // Global metadata structures
3431
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003432 // struct _objc_symtab {
3433 // long sel_ref_cnt;
3434 // SEL *refs;
3435 // short cls_def_cnt;
3436 // short cat_def_cnt;
3437 // char *defs[cls_def_cnt + cat_def_cnt];
3438 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003439 SymtabTy = llvm::StructType::get(LongTy,
3440 SelectorPtrTy,
3441 ShortTy,
3442 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003443 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444 NULL);
3445 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3446 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3447
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003448 // struct _objc_module {
3449 // long version;
3450 // long size; // sizeof(struct _objc_module)
3451 // char *name;
3452 // struct _objc_symtab* symtab;
3453 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003454 ModuleTy =
3455 llvm::StructType::get(LongTy,
3456 LongTy,
3457 Int8PtrTy,
3458 SymtabPtrTy,
3459 NULL);
3460 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003461
Daniel Dunbar49f66022008-09-24 03:38:44 +00003462 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003463
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003464 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003465 std::vector<const llvm::Type*> Params;
3466 Params.push_back(ObjectPtrTy);
3467 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003468 MessageSendFn =
3469 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3470 Params,
3471 true),
3472 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003473
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003474 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003475 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003476 Params.push_back(ObjectPtrTy);
3477 Params.push_back(SelectorPtrTy);
3478 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003479 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3480 Params,
3481 true),
3482 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003483
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003484 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003485 Params.clear();
3486 Params.push_back(ObjectPtrTy);
3487 Params.push_back(SelectorPtrTy);
3488 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003489 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003490 MessageSendFpretFn =
3491 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3492 Params,
3493 true),
3494 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003495
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003496 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003497 Params.clear();
3498 Params.push_back(SuperPtrTy);
3499 Params.push_back(SelectorPtrTy);
3500 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003501 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3502 Params,
3503 true),
3504 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003505
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003506 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3507 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003508 Params.clear();
3509 Params.push_back(Int8PtrTy);
3510 Params.push_back(SuperPtrTy);
3511 Params.push_back(SelectorPtrTy);
3512 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003513 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3514 Params,
3515 true),
3516 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003517
3518 // There is no objc_msgSendSuper_fpret? How can that work?
3519 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003520
Anders Carlsson124526b2008-09-09 10:10:21 +00003521 // FIXME: This is the size of the setjmp buffer and should be
3522 // target specific. 18 is what's used on 32-bit X86.
3523 uint64_t SetJmpBufferSize = 18;
3524
3525 // Exceptions
3526 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003527 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003528
3529 ExceptionDataTy =
3530 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3531 SetJmpBufferSize),
3532 StackPtrTy, NULL);
3533 CGM.getModule().addTypeName("struct._objc_exception_data",
3534 ExceptionDataTy);
3535
3536 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003537 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3538 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003539 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3540 Params,
3541 false),
3542 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003543 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003544 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3545 Params,
3546 false),
3547 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003548 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003549 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3550 Params,
3551 false),
3552 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003553
3554 Params.clear();
3555 Params.push_back(ClassPtrTy);
3556 Params.push_back(ObjectPtrTy);
3557 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003558 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3559 Params,
3560 false),
3561 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003562
Anders Carlsson124526b2008-09-09 10:10:21 +00003563 Params.clear();
3564 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3565 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003566 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3567 Params,
3568 false),
3569 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003570
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003571}
3572
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003573ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003574: ObjCCommonTypesHelper(cgm)
3575{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003576 // struct _method_list_t {
3577 // uint32_t entsize; // sizeof(struct _objc_method)
3578 // uint32_t method_count;
3579 // struct _objc_method method_list[method_count];
3580 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003581 MethodListnfABITy = llvm::StructType::get(IntTy,
3582 IntTy,
3583 llvm::ArrayType::get(MethodTy, 0),
3584 NULL);
3585 CGM.getModule().addTypeName("struct.__method_list_t",
3586 MethodListnfABITy);
3587 // struct method_list_t *
3588 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003589
3590 // struct _protocol_t {
3591 // id isa; // NULL
3592 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003593 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003594 // const struct method_list_t * const instance_methods;
3595 // const struct method_list_t * const class_methods;
3596 // const struct method_list_t *optionalInstanceMethods;
3597 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003598 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003599 // const uint32_t size; // sizeof(struct _protocol_t)
3600 // const uint32_t flags; // = 0
3601 // }
3602
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003603 // Holder for struct _protocol_list_t *
3604 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3605
3606 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3607 Int8PtrTy,
3608 llvm::PointerType::getUnqual(
3609 ProtocolListTyHolder),
3610 MethodListnfABIPtrTy,
3611 MethodListnfABIPtrTy,
3612 MethodListnfABIPtrTy,
3613 MethodListnfABIPtrTy,
3614 PropertyListPtrTy,
3615 IntTy,
3616 IntTy,
3617 NULL);
3618 CGM.getModule().addTypeName("struct._protocol_t",
3619 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003620
3621 // struct _protocol_t*
3622 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003623
Fariborz Jahanianda320092009-01-29 19:24:30 +00003624 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003625 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003626 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003627 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003628 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3629 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003630 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003631 NULL);
3632 CGM.getModule().addTypeName("struct._objc_protocol_list",
3633 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003634 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3635 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003637 // struct _objc_protocol_list*
3638 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003639
3640 // struct _ivar_t {
3641 // unsigned long int *offset; // pointer to ivar offset location
3642 // char *name;
3643 // char *type;
3644 // uint32_t alignment;
3645 // uint32_t size;
3646 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003647 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3648 Int8PtrTy,
3649 Int8PtrTy,
3650 IntTy,
3651 IntTy,
3652 NULL);
3653 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3654
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003655 // struct _ivar_list_t {
3656 // uint32 entsize; // sizeof(struct _ivar_t)
3657 // uint32 count;
3658 // struct _iver_t list[count];
3659 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003660 IvarListnfABITy = llvm::StructType::get(IntTy,
3661 IntTy,
3662 llvm::ArrayType::get(
3663 IvarnfABITy, 0),
3664 NULL);
3665 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3666
3667 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003668
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003669 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003670 // uint32_t const flags;
3671 // uint32_t const instanceStart;
3672 // uint32_t const instanceSize;
3673 // uint32_t const reserved; // only when building for 64bit targets
3674 // const uint8_t * const ivarLayout;
3675 // const char *const name;
3676 // const struct _method_list_t * const baseMethods;
3677 // const struct _objc_protocol_list *const baseProtocols;
3678 // const struct _ivar_list_t *const ivars;
3679 // const uint8_t * const weakIvarLayout;
3680 // const struct _prop_list_t * const properties;
3681 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003682
3683 // FIXME. Add 'reserved' field in 64bit abi mode!
3684 ClassRonfABITy = llvm::StructType::get(IntTy,
3685 IntTy,
3686 IntTy,
3687 Int8PtrTy,
3688 Int8PtrTy,
3689 MethodListnfABIPtrTy,
3690 ProtocolListnfABIPtrTy,
3691 IvarListnfABIPtrTy,
3692 Int8PtrTy,
3693 PropertyListPtrTy,
3694 NULL);
3695 CGM.getModule().addTypeName("struct._class_ro_t",
3696 ClassRonfABITy);
3697
3698 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3699 std::vector<const llvm::Type*> Params;
3700 Params.push_back(ObjectPtrTy);
3701 Params.push_back(SelectorPtrTy);
3702 ImpnfABITy = llvm::PointerType::getUnqual(
3703 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3704
3705 // struct _class_t {
3706 // struct _class_t *isa;
3707 // struct _class_t * const superclass;
3708 // void *cache;
3709 // IMP *vtable;
3710 // struct class_ro_t *ro;
3711 // }
3712
3713 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3714 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3715 llvm::PointerType::getUnqual(ClassTyHolder),
3716 CachePtrTy,
3717 llvm::PointerType::getUnqual(ImpnfABITy),
3718 llvm::PointerType::getUnqual(
3719 ClassRonfABITy),
3720 NULL);
3721 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3722
3723 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3724 ClassnfABITy);
3725
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003726 // LLVM for struct _class_t *
3727 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3728
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003729 // struct _category_t {
3730 // const char * const name;
3731 // struct _class_t *const cls;
3732 // const struct _method_list_t * const instance_methods;
3733 // const struct _method_list_t * const class_methods;
3734 // const struct _protocol_list_t * const protocols;
3735 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003736 // }
3737 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003738 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003739 MethodListnfABIPtrTy,
3740 MethodListnfABIPtrTy,
3741 ProtocolListnfABIPtrTy,
3742 PropertyListPtrTy,
3743 NULL);
3744 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003745
3746 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003747 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3748 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003749
3750 // MessageRefTy - LLVM for:
3751 // struct _message_ref_t {
3752 // IMP messenger;
3753 // SEL name;
3754 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003755
3756 // First the clang type for struct _message_ref_t
3757 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3758 SourceLocation(),
3759 &Ctx.Idents.get("_message_ref_t"));
3760 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3761 Ctx.VoidPtrTy, 0, false));
3762 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3763 Ctx.getObjCSelType(), 0, false));
3764 RD->completeDefinition(Ctx);
3765
3766 MessageRefCTy = Ctx.getTagDeclType(RD);
3767 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3768 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003769
3770 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3771 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3772
3773 // SuperMessageRefTy - LLVM for:
3774 // struct _super_message_ref_t {
3775 // SUPER_IMP messenger;
3776 // SEL name;
3777 // };
3778 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3779 SelectorPtrTy,
3780 NULL);
3781 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3782
3783 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3784 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3785
3786 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3787 Params.clear();
3788 Params.push_back(ObjectPtrTy);
3789 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003790 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3791 Params,
3792 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003793 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003794 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003795 "objc_msgSend_fixup");
3796
3797 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3798 MessageSendFpretFixupFn =
3799 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3800 Params,
3801 true),
3802 "objc_msgSend_fpret_fixup");
3803
3804 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3805 MessageSendStretFixupFn =
3806 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3807 Params,
3808 true),
3809 "objc_msgSend_stret_fixup");
3810
3811 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3812 MessageSendIdFixupFn =
3813 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3814 Params,
3815 true),
3816 "objc_msgSendId_fixup");
3817
3818
3819 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3820 MessageSendIdStretFixupFn =
3821 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3822 Params,
3823 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003824 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003825
3826 // id objc_msgSendSuper2_fixup (struct objc_super *,
3827 // struct _super_message_ref_t*, ...)
3828 Params.clear();
3829 Params.push_back(SuperPtrTy);
3830 Params.push_back(SuperMessageRefPtrTy);
3831 MessageSendSuper2FixupFn =
3832 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3833 Params,
3834 true),
3835 "objc_msgSendSuper2_fixup");
3836
3837
3838 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3839 // struct _super_message_ref_t*, ...)
3840 MessageSendSuper2StretFixupFn =
3841 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3842 Params,
3843 true),
3844 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003845
3846 Params.clear();
3847 llvm::Constant *Personality =
3848 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3849 Params,
3850 true),
3851 "__objc_personality_v0");
3852 EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
3853
3854 Params.clear();
3855 Params.push_back(Int8PtrTy);
3856 UnwindResumeOrRethrowFn =
3857 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3858 Params,
3859 false),
3860 "_Unwind_Resume_or_Rethrow");
Daniel Dunbare588b992009-03-01 04:46:24 +00003861 ObjCBeginCatchFn =
3862 CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
3863 Params,
3864 false),
3865 "objc_begin_catch");
3866
3867 Params.clear();
3868 ObjCEndCatchFn =
3869 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3870 Params,
3871 false),
3872 "objc_end_catch");
3873
3874 // struct objc_typeinfo {
3875 // const void** vtable; // objc_ehtype_vtable + 2
3876 // const char* name; // c++ typeinfo string
3877 // Class cls;
3878 // };
3879 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3880 Int8PtrTy,
3881 ClassnfABIPtrTy,
3882 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003883 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003884 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003885}
3886
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003887llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3888 FinishNonFragileABIModule();
3889
3890 return NULL;
3891}
3892
3893void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3894 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003895
3896 // Build list of all implemented classe addresses in array
3897 // L_OBJC_LABEL_CLASS_$.
3898 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3899 // list of 'nonlazy' implementations (defined as those with a +load{}
3900 // method!!).
3901 unsigned NumClasses = DefinedClasses.size();
3902 if (NumClasses) {
3903 std::vector<llvm::Constant*> Symbols(NumClasses);
3904 for (unsigned i=0; i<NumClasses; i++)
3905 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3906 ObjCTypes.Int8PtrTy);
3907 llvm::Constant* Init =
3908 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3909 NumClasses),
3910 Symbols);
3911
3912 llvm::GlobalVariable *GV =
3913 new llvm::GlobalVariable(Init->getType(), false,
3914 llvm::GlobalValue::InternalLinkage,
3915 Init,
3916 "\01L_OBJC_LABEL_CLASS_$",
3917 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003918 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003919 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3920 UsedGlobals.push_back(GV);
3921 }
3922
3923 // Build list of all implemented category addresses in array
3924 // L_OBJC_LABEL_CATEGORY_$.
3925 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3926 // list of 'nonlazy' category implementations (defined as those with a +load{}
3927 // method!!).
3928 unsigned NumCategory = DefinedCategories.size();
3929 if (NumCategory) {
3930 std::vector<llvm::Constant*> Symbols(NumCategory);
3931 for (unsigned i=0; i<NumCategory; i++)
3932 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3933 ObjCTypes.Int8PtrTy);
3934 llvm::Constant* Init =
3935 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3936 NumCategory),
3937 Symbols);
3938
3939 llvm::GlobalVariable *GV =
3940 new llvm::GlobalVariable(Init->getType(), false,
3941 llvm::GlobalValue::InternalLinkage,
3942 Init,
3943 "\01L_OBJC_LABEL_CATEGORY_$",
3944 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003945 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003946 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3947 UsedGlobals.push_back(GV);
3948 }
3949
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003950 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3951 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3952 std::vector<llvm::Constant*> Values(2);
3953 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003954 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00003955 // FIXME: Fix and continue?
3956 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3957 flags |= eImageInfo_GarbageCollected;
3958 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3959 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003960 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003961 llvm::Constant* Init = llvm::ConstantArray::get(
3962 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3963 Values);
3964 llvm::GlobalVariable *IMGV =
3965 new llvm::GlobalVariable(Init->getType(), false,
3966 llvm::GlobalValue::InternalLinkage,
3967 Init,
3968 "\01L_OBJC_IMAGE_INFO",
3969 &CGM.getModule());
3970 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3971 UsedGlobals.push_back(IMGV);
3972
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003973 std::vector<llvm::Constant*> Used;
3974 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3975 e = UsedGlobals.end(); i != e; ++i) {
3976 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3977 }
3978
3979 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3980 llvm::GlobalValue *GV =
3981 new llvm::GlobalVariable(AT, false,
3982 llvm::GlobalValue::AppendingLinkage,
3983 llvm::ConstantArray::get(AT, Used),
3984 "llvm.used",
3985 &CGM.getModule());
3986
3987 GV->setSection("llvm.metadata");
3988
3989}
3990
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003991// Metadata flags
3992enum MetaDataDlags {
3993 CLS = 0x0,
3994 CLS_META = 0x1,
3995 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003996 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003997 CLS_EXCEPTION = 0x20
3998};
3999/// BuildClassRoTInitializer - generate meta-data for:
4000/// struct _class_ro_t {
4001/// uint32_t const flags;
4002/// uint32_t const instanceStart;
4003/// uint32_t const instanceSize;
4004/// uint32_t const reserved; // only when building for 64bit targets
4005/// const uint8_t * const ivarLayout;
4006/// const char *const name;
4007/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004008/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004009/// const struct _ivar_list_t *const ivars;
4010/// const uint8_t * const weakIvarLayout;
4011/// const struct _prop_list_t * const properties;
4012/// }
4013///
4014llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4015 unsigned flags,
4016 unsigned InstanceStart,
4017 unsigned InstanceSize,
4018 const ObjCImplementationDecl *ID) {
4019 std::string ClassName = ID->getNameAsString();
4020 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4021 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4022 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4023 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4024 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00004025 // FIXME. ivarLayout is currently null!
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004026 Values[ 3] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004027 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004028 // const struct _method_list_t * const baseMethods;
4029 std::vector<llvm::Constant*> Methods;
4030 std::string MethodListName("\01l_OBJC_$_");
4031 if (flags & CLS_META) {
4032 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4033 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
4034 e = ID->classmeth_end(); i != e; ++i) {
4035 // Class methods should always be defined.
4036 Methods.push_back(GetMethodConstant(*i));
4037 }
4038 } else {
4039 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4040 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
4041 e = ID->instmeth_end(); i != e; ++i) {
4042 // Instance methods should always be defined.
4043 Methods.push_back(GetMethodConstant(*i));
4044 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004045 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
4046 e = ID->propimpl_end(); i != e; ++i) {
4047 ObjCPropertyImplDecl *PID = *i;
4048
4049 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4050 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4051
4052 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4053 if (llvm::Constant *C = GetMethodConstant(MD))
4054 Methods.push_back(C);
4055 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4056 if (llvm::Constant *C = GetMethodConstant(MD))
4057 Methods.push_back(C);
4058 }
4059 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004060 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004061 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004062 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004063
4064 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4065 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4066 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4067 + OID->getNameAsString(),
4068 OID->protocol_begin(),
4069 OID->protocol_end());
4070
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004071 if (flags & CLS_META)
4072 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4073 else
4074 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004075 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004076 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004077 if (flags & CLS_META)
4078 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4079 else
4080 Values[ 9] =
4081 EmitPropertyList(
4082 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4083 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004084 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4085 Values);
4086 llvm::GlobalVariable *CLASS_RO_GV =
4087 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4088 llvm::GlobalValue::InternalLinkage,
4089 Init,
4090 (flags & CLS_META) ?
4091 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4092 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4093 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004094 CLASS_RO_GV->setAlignment(
4095 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004096 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004097 UsedGlobals.push_back(CLASS_RO_GV);
4098 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004099
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004100}
4101
4102/// BuildClassMetaData - This routine defines that to-level meta-data
4103/// for the given ClassName for:
4104/// struct _class_t {
4105/// struct _class_t *isa;
4106/// struct _class_t * const superclass;
4107/// void *cache;
4108/// IMP *vtable;
4109/// struct class_ro_t *ro;
4110/// }
4111///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004112llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4113 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004114 llvm::Constant *IsAGV,
4115 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004116 llvm::Constant *ClassRoGV,
4117 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004118 std::vector<llvm::Constant*> Values(5);
4119 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004120 Values[1] = SuperClassGV
4121 ? SuperClassGV
4122 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004123 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4124 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4125 Values[4] = ClassRoGV; // &CLASS_RO_GV
4126 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4127 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004128 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4129 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004130 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004131 GV->setAlignment(
4132 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004133 if (HiddenVisibility)
4134 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004135 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004136 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004137}
4138
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004139void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4140 std::string ClassName = ID->getNameAsString();
4141 if (!ObjCEmptyCacheVar) {
4142 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004143 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004144 false,
4145 llvm::GlobalValue::ExternalLinkage,
4146 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00004147 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004148 &CGM.getModule());
4149 UsedGlobals.push_back(ObjCEmptyCacheVar);
4150
4151 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004152 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004153 false,
4154 llvm::GlobalValue::ExternalLinkage,
4155 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00004156 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004157 &CGM.getModule());
4158 UsedGlobals.push_back(ObjCEmptyVtableVar);
4159 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004160 assert(ID->getClassInterface() &&
4161 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004162 uint32_t InstanceStart =
4163 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4164 uint32_t InstanceSize = InstanceStart;
4165 uint32_t flags = CLS_META;
4166 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
4167 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004168
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004169 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004170
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004171 bool classIsHidden = IsClassHidden(ID->getClassInterface());
4172 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004173 flags |= OBJC2_CLS_HIDDEN;
4174 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004175 // class is root
4176 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004177 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
4178 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004179 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004180 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004181 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4182 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4183 Root = Super;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004184 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004185 // work on super class metadata symbol.
4186 std::string SuperClassName =
4187 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004188 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004189 }
4190 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4191 InstanceStart,
4192 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004193 std::string TClassName = ObjCMetaClassName + ClassName;
4194 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004195 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4196 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004197
4198 // Metadata for the class
4199 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004200 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004201 flags |= OBJC2_CLS_HIDDEN;
4202 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004203 flags |= CLS_ROOT;
4204 SuperClassGV = 0;
4205 }
4206 else {
4207 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004208 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004209 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004210 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004211 }
Fariborz Jahanianebf9ed32009-03-20 20:48:19 +00004212 // FIXME: Gross
4213 ObjCInterfaceDecl *Interface =
4214 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
4215 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004216 InstanceStart = InstanceSize = 0;
4217 if (ObjCInterfaceDecl *OID =
4218 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
4219 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004220 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004221
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004222 RecordDecl::field_iterator firstField, lastField;
4223 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004224
4225 for (RecordDecl::field_iterator e = RD->field_end(),
4226 ifield = firstField; ifield != e; ++ifield)
4227 lastField = ifield;
4228
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004229 if (lastField != RD->field_end()) {
4230 FieldDecl *Field = *lastField;
4231 const llvm::Type *FieldTy =
4232 CGM.getTypes().ConvertTypeForMem(Field->getType());
4233 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004234 InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004235 if (firstField == RD->field_end())
4236 InstanceStart = InstanceSize;
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004237 else {
4238 Field = *firstField;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004239 InstanceStart = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004240 }
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004241 }
4242 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004243 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004244 InstanceStart,
4245 InstanceSize,
4246 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004247
4248 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004249 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004250 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4251 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004252 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004253}
4254
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004255/// GenerateProtocolRef - This routine is called to generate code for
4256/// a protocol reference expression; as in:
4257/// @code
4258/// @protocol(Proto1);
4259/// @endcode
4260/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4261/// which will hold address of the protocol meta-data.
4262///
4263llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4264 const ObjCProtocolDecl *PD) {
4265
4266 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
4267 ObjCTypes.ExternalProtocolPtrTy);
4268
4269 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4270 ProtocolName += PD->getNameAsCString();
4271
4272 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4273 if (PTGV)
4274 return Builder.CreateLoad(PTGV, false, "tmp");
4275 PTGV = new llvm::GlobalVariable(
4276 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004277 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004278 Init,
4279 ProtocolName,
4280 &CGM.getModule());
4281 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4282 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4283 UsedGlobals.push_back(PTGV);
4284 return Builder.CreateLoad(PTGV, false, "tmp");
4285}
4286
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004287/// GenerateCategory - Build metadata for a category implementation.
4288/// struct _category_t {
4289/// const char * const name;
4290/// struct _class_t *const cls;
4291/// const struct _method_list_t * const instance_methods;
4292/// const struct _method_list_t * const class_methods;
4293/// const struct _protocol_list_t * const protocols;
4294/// const struct _prop_list_t * const properties;
4295/// }
4296///
4297void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4298{
4299 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004300 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4301 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004302 "_$_" + OCD->getNameAsString());
4303 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
4304
4305 std::vector<llvm::Constant*> Values(6);
4306 Values[0] = GetClassName(OCD->getIdentifier());
4307 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004308 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004309 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004310 std::vector<llvm::Constant*> Methods;
4311 std::string MethodListName(Prefix);
4312 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4313 "_$_" + OCD->getNameAsString();
4314
4315 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
4316 e = OCD->instmeth_end(); i != e; ++i) {
4317 // Instance methods should always be defined.
4318 Methods.push_back(GetMethodConstant(*i));
4319 }
4320
4321 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004322 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004323 Methods);
4324
4325 MethodListName = Prefix;
4326 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4327 OCD->getNameAsString();
4328 Methods.clear();
4329 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
4330 e = OCD->classmeth_end(); i != e; ++i) {
4331 // Class methods should always be defined.
4332 Methods.push_back(GetMethodConstant(*i));
4333 }
4334
4335 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004336 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004337 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004338 const ObjCCategoryDecl *Category =
4339 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004340 if (Category) {
4341 std::string ExtName(Interface->getNameAsString() + "_$_" +
4342 OCD->getNameAsString());
4343 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4344 + Interface->getNameAsString() + "_$_"
4345 + Category->getNameAsString(),
4346 Category->protocol_begin(),
4347 Category->protocol_end());
4348 Values[5] =
4349 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4350 OCD, Category, ObjCTypes);
4351 }
4352 else {
4353 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4354 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4355 }
4356
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004357 llvm::Constant *Init =
4358 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4359 Values);
4360 llvm::GlobalVariable *GCATV
4361 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4362 false,
4363 llvm::GlobalValue::InternalLinkage,
4364 Init,
4365 ExtCatName,
4366 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004367 GCATV->setAlignment(
4368 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004369 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004370 UsedGlobals.push_back(GCATV);
4371 DefinedCategories.push_back(GCATV);
4372}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004373
4374/// GetMethodConstant - Return a struct objc_method constant for the
4375/// given method if it has been defined. The result is null if the
4376/// method has not been defined. The return value has type MethodPtrTy.
4377llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4378 const ObjCMethodDecl *MD) {
4379 // FIXME: Use DenseMap::lookup
4380 llvm::Function *Fn = MethodDefinitions[MD];
4381 if (!Fn)
4382 return 0;
4383
4384 std::vector<llvm::Constant*> Method(3);
4385 Method[0] =
4386 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4387 ObjCTypes.SelectorPtrTy);
4388 Method[1] = GetMethodVarType(MD);
4389 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4390 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4391}
4392
4393/// EmitMethodList - Build meta-data for method declarations
4394/// struct _method_list_t {
4395/// uint32_t entsize; // sizeof(struct _objc_method)
4396/// uint32_t method_count;
4397/// struct _objc_method method_list[method_count];
4398/// }
4399///
4400llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4401 const std::string &Name,
4402 const char *Section,
4403 const ConstantVector &Methods) {
4404 // Return null for empty list.
4405 if (Methods.empty())
4406 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4407
4408 std::vector<llvm::Constant*> Values(3);
4409 // sizeof(struct _objc_method)
4410 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4411 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4412 // method_count
4413 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4414 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4415 Methods.size());
4416 Values[2] = llvm::ConstantArray::get(AT, Methods);
4417 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4418
4419 llvm::GlobalVariable *GV =
4420 new llvm::GlobalVariable(Init->getType(), false,
4421 llvm::GlobalValue::InternalLinkage,
4422 Init,
4423 Name,
4424 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004425 GV->setAlignment(
4426 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004427 GV->setSection(Section);
4428 UsedGlobals.push_back(GV);
4429 return llvm::ConstantExpr::getBitCast(GV,
4430 ObjCTypes.MethodListnfABIPtrTy);
4431}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004432
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004433/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4434/// the given ivar.
4435///
4436llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
4437 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004438 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004439 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004440 Name += "\01_OBJC_IVAR_$_" +
4441 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004442 + Ivar->getNameAsString();
4443 llvm::GlobalVariable *IvarOffsetGV =
4444 CGM.getModule().getGlobalVariable(Name);
4445 if (!IvarOffsetGV)
4446 IvarOffsetGV =
4447 new llvm::GlobalVariable(ObjCTypes.LongTy,
4448 false,
4449 llvm::GlobalValue::ExternalLinkage,
4450 0,
4451 Name,
4452 &CGM.getModule());
4453 return IvarOffsetGV;
4454}
4455
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004456llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004457 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004458 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004459 unsigned long int Offset) {
4460
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004461 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004462 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004463 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004464 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004465
4466 llvm::GlobalVariable *IvarOffsetGV =
4467 CGM.getModule().getGlobalVariable(ExternalName);
4468 if (IvarOffsetGV) {
4469 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004470 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004471 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004472 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004473 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004474 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004475 return IvarOffsetGV;
4476 }
4477
4478 IvarOffsetGV =
4479 new llvm::GlobalVariable(Init->getType(),
4480 false,
4481 llvm::GlobalValue::ExternalLinkage,
4482 Init,
4483 ExternalName,
4484 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004485 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004486 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004487 // @private and @package have hidden visibility.
4488 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4489 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4490 if (!globalVisibility)
4491 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004492 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004493 if (IsClassHidden(ID))
4494 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004495
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004496 IvarOffsetGV->setSection("__DATA, __objc_const");
4497 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004498 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004499}
4500
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004501/// EmitIvarList - Emit the ivar list for the given
4502/// implementation. If ForClass is true the list of class ivars
4503/// (i.e. metaclass ivars) is emitted, otherwise the list of
4504/// interface ivars will be emitted. The return value has type
4505/// IvarListnfABIPtrTy.
4506/// struct _ivar_t {
4507/// unsigned long int *offset; // pointer to ivar offset location
4508/// char *name;
4509/// char *type;
4510/// uint32_t alignment;
4511/// uint32_t size;
4512/// }
4513/// struct _ivar_list_t {
4514/// uint32 entsize; // sizeof(struct _ivar_t)
4515/// uint32 count;
4516/// struct _iver_t list[count];
4517/// }
4518///
4519llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4520 const ObjCImplementationDecl *ID) {
4521
4522 std::vector<llvm::Constant*> Ivars, Ivar(5);
4523
4524 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4525 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4526
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004527 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004528 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004529
4530 RecordDecl::field_iterator i,p;
4531 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004532 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4533
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004534 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004535 FieldDecl *Field = *i;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004536 uint64_t offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004537 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004538 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004539 if (Field->getIdentifier())
4540 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4541 else
4542 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00004543 Ivar[2] = GetMethodVarType(Field);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004544 const llvm::Type *FieldTy =
4545 CGM.getTypes().ConvertTypeForMem(Field->getType());
4546 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4547 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4548 Field->getType().getTypePtr()) >> 3;
4549 Align = llvm::Log2_32(Align);
4550 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004551 // NOTE. Size of a bitfield does not match gcc's, because of the way
4552 // bitfields are treated special in each. But I am told that 'size'
4553 // for bitfield ivars is ignored by the runtime so it does not matter.
4554 // (even if it matters, some day, there is enough info. to get the bitfield
4555 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004556 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4557 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4558 }
4559 // Return null for empty list.
4560 if (Ivars.empty())
4561 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4562 std::vector<llvm::Constant*> Values(3);
4563 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4564 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4565 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4566 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4567 Ivars.size());
4568 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4569 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4570 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4571 llvm::GlobalVariable *GV =
4572 new llvm::GlobalVariable(Init->getType(), false,
4573 llvm::GlobalValue::InternalLinkage,
4574 Init,
4575 Prefix + OID->getNameAsString(),
4576 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004577 GV->setAlignment(
4578 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004579 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004580
4581 UsedGlobals.push_back(GV);
4582 return llvm::ConstantExpr::getBitCast(GV,
4583 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004584}
4585
4586llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4587 const ObjCProtocolDecl *PD) {
4588 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4589
4590 if (!Entry) {
4591 // We use the initializer as a marker of whether this is a forward
4592 // reference or not. At module finalization we add the empty
4593 // contents for protocols which were referenced but never defined.
4594 Entry =
4595 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4596 llvm::GlobalValue::ExternalLinkage,
4597 0,
4598 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4599 &CGM.getModule());
4600 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4601 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004602 }
4603
4604 return Entry;
4605}
4606
4607/// GetOrEmitProtocol - Generate the protocol meta-data:
4608/// @code
4609/// struct _protocol_t {
4610/// id isa; // NULL
4611/// const char * const protocol_name;
4612/// const struct _protocol_list_t * protocol_list; // super protocols
4613/// const struct method_list_t * const instance_methods;
4614/// const struct method_list_t * const class_methods;
4615/// const struct method_list_t *optionalInstanceMethods;
4616/// const struct method_list_t *optionalClassMethods;
4617/// const struct _prop_list_t * properties;
4618/// const uint32_t size; // sizeof(struct _protocol_t)
4619/// const uint32_t flags; // = 0
4620/// }
4621/// @endcode
4622///
4623
4624llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4625 const ObjCProtocolDecl *PD) {
4626 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4627
4628 // Early exit if a defining object has already been generated.
4629 if (Entry && Entry->hasInitializer())
4630 return Entry;
4631
4632 const char *ProtocolName = PD->getNameAsCString();
4633
4634 // Construct method lists.
4635 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4636 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4637 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4638 e = PD->instmeth_end(); i != e; ++i) {
4639 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004640 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004641 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4642 OptInstanceMethods.push_back(C);
4643 } else {
4644 InstanceMethods.push_back(C);
4645 }
4646 }
4647
4648 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4649 e = PD->classmeth_end(); i != e; ++i) {
4650 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004651 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004652 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4653 OptClassMethods.push_back(C);
4654 } else {
4655 ClassMethods.push_back(C);
4656 }
4657 }
4658
4659 std::vector<llvm::Constant*> Values(10);
4660 // isa is NULL
4661 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4662 Values[1] = GetClassName(PD->getIdentifier());
4663 Values[2] = EmitProtocolList(
4664 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4665 PD->protocol_begin(),
4666 PD->protocol_end());
4667
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004668 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004669 + PD->getNameAsString(),
4670 "__DATA, __objc_const",
4671 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004672 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004673 + PD->getNameAsString(),
4674 "__DATA, __objc_const",
4675 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004676 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004677 + PD->getNameAsString(),
4678 "__DATA, __objc_const",
4679 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004680 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004681 + PD->getNameAsString(),
4682 "__DATA, __objc_const",
4683 OptClassMethods);
4684 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4685 0, PD, ObjCTypes);
4686 uint32_t Size =
4687 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4688 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4689 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4690 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4691 Values);
4692
4693 if (Entry) {
4694 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004695 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004696 Entry->setInitializer(Init);
4697 } else {
4698 Entry =
4699 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004700 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004701 Init,
4702 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4703 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004704 Entry->setAlignment(
4705 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004706 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004707 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004708 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4709
4710 // Use this protocol meta-data to build protocol list table in section
4711 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004712 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004713 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004714 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004715 Entry,
4716 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4717 +ProtocolName,
4718 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004719 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004720 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004721 PTGV->setSection("__DATA, __objc_protolist");
4722 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4723 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004724 return Entry;
4725}
4726
4727/// EmitProtocolList - Generate protocol list meta-data:
4728/// @code
4729/// struct _protocol_list_t {
4730/// long protocol_count; // Note, this is 32/64 bit
4731/// struct _protocol_t[protocol_count];
4732/// }
4733/// @endcode
4734///
4735llvm::Constant *
4736CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4737 ObjCProtocolDecl::protocol_iterator begin,
4738 ObjCProtocolDecl::protocol_iterator end) {
4739 std::vector<llvm::Constant*> ProtocolRefs;
4740
Fariborz Jahanianda320092009-01-29 19:24:30 +00004741 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004742 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004743 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4744
Daniel Dunbar948e2582009-02-15 07:36:20 +00004745 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004746 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4747 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004748 return llvm::ConstantExpr::getBitCast(GV,
4749 ObjCTypes.ProtocolListnfABIPtrTy);
4750
4751 for (; begin != end; ++begin)
4752 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4753
Fariborz Jahanianda320092009-01-29 19:24:30 +00004754 // This list is null terminated.
4755 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004756 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004757
4758 std::vector<llvm::Constant*> Values(2);
4759 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4760 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004761 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004762 ProtocolRefs.size()),
4763 ProtocolRefs);
4764
4765 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4766 GV = new llvm::GlobalVariable(Init->getType(), false,
4767 llvm::GlobalValue::InternalLinkage,
4768 Init,
4769 Name,
4770 &CGM.getModule());
4771 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004772 GV->setAlignment(
4773 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004774 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004775 return llvm::ConstantExpr::getBitCast(GV,
4776 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004777}
4778
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004779/// GetMethodDescriptionConstant - This routine build following meta-data:
4780/// struct _objc_method {
4781/// SEL _cmd;
4782/// char *method_type;
4783/// char *_imp;
4784/// }
4785
4786llvm::Constant *
4787CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4788 std::vector<llvm::Constant*> Desc(3);
4789 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4790 ObjCTypes.SelectorPtrTy);
4791 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004792 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004793 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4794 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4795}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004796
4797/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4798/// This code gen. amounts to generating code for:
4799/// @code
4800/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4801/// @encode
4802///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004803LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004804 CodeGen::CodeGenFunction &CGF,
4805 QualType ObjectTy,
4806 llvm::Value *BaseValue,
4807 const ObjCIvarDecl *Ivar,
4808 const FieldDecl *Field,
4809 unsigned CVRQualifiers) {
4810 assert(ObjectTy->isObjCInterfaceType() &&
4811 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004812 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004813 std::string ExternalName;
4814 llvm::GlobalVariable *IvarOffsetGV =
4815 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004816
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004817 // (char *) BaseValue
4818 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4819 ObjCTypes.Int8PtrTy);
4820 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4821 // (char*)BaseValue + Offset_symbol
4822 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4823 // (type *)((char*)BaseValue + Offset_symbol)
4824 const llvm::Type *IvarTy =
4825 CGM.getTypes().ConvertType(Ivar->getType());
4826 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4827 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004828
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004829 if (Ivar->isBitField()) {
4830 CodeGenTypes::BitFieldInfo bitFieldInfo =
4831 CGM.getTypes().getBitFieldInfo(Field);
4832 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
4833 Field->getType()->isSignedIntegerType(),
4834 Field->getType().getCVRQualifiers()|CVRQualifiers);
4835 }
4836
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004837 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004838 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4839 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004840 LValue::SetObjCIvar(LV, true);
4841 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004842}
4843
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004844llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4845 CodeGen::CodeGenFunction &CGF,
4846 ObjCInterfaceDecl *Interface,
4847 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004848 std::string ExternalName;
4849 llvm::GlobalVariable *IvarOffsetGV =
4850 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4851
4852 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004853}
4854
Fariborz Jahanian46551122009-02-04 00:22:57 +00004855CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4856 CodeGen::CodeGenFunction &CGF,
4857 QualType ResultType,
4858 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004859 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004860 QualType Arg0Ty,
4861 bool IsSuper,
4862 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004863 // FIXME. Even though IsSuper is passes. This function doese not
4864 // handle calls to 'super' receivers.
4865 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004866 llvm::Value *Arg0 = Receiver;
4867 if (!IsSuper)
4868 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004869
4870 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004871 // FIXME. This is too much work to get the ABI-specific result type
4872 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004873 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4874 llvm::SmallVector<QualType, 16>());
4875 llvm::Constant *Fn;
4876 std::string Name("\01l_");
4877 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004878#if 0
4879 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004880 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4881 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4882 // FIXME. Is there a better way of getting these names.
4883 // They are available in RuntimeFunctions vector pair.
4884 Name += "objc_msgSendId_stret_fixup";
4885 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004886 else
4887#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004888 if (IsSuper) {
4889 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4890 Name += "objc_msgSendSuper2_stret_fixup";
4891 }
4892 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004893 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004894 Fn = ObjCTypes.MessageSendStretFixupFn;
4895 Name += "objc_msgSend_stret_fixup";
4896 }
4897 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004898 else if (ResultType->isFloatingType() &&
4899 // Selection of frret API only happens in 32bit nonfragile ABI.
4900 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004901 Fn = ObjCTypes.MessageSendFpretFixupFn;
4902 Name += "objc_msgSend_fpret_fixup";
4903 }
4904 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004905#if 0
4906// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004907 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4908 Fn = ObjCTypes.MessageSendIdFixupFn;
4909 Name += "objc_msgSendId_fixup";
4910 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004911 else
4912#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004913 if (IsSuper) {
4914 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4915 Name += "objc_msgSendSuper2_fixup";
4916 }
4917 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004918 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004919 Fn = ObjCTypes.MessageSendFixupFn;
4920 Name += "objc_msgSend_fixup";
4921 }
4922 }
4923 Name += '_';
4924 std::string SelName(Sel.getAsString());
4925 // Replace all ':' in selector name with '_' ouch!
4926 for(unsigned i = 0; i < SelName.size(); i++)
4927 if (SelName[i] == ':')
4928 SelName[i] = '_';
4929 Name += SelName;
4930 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4931 if (!GV) {
4932 // Build messafe ref table entry.
4933 std::vector<llvm::Constant*> Values(2);
4934 Values[0] = Fn;
4935 Values[1] = GetMethodVarName(Sel);
4936 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4937 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004938 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004939 Init,
4940 Name,
4941 &CGM.getModule());
4942 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4943 GV->setAlignment(
4944 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4945 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4946 UsedGlobals.push_back(GV);
4947 }
4948 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004949
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004950 CallArgList ActualArgs;
4951 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4952 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4953 ObjCTypes.MessageRefCPtrTy));
4954 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004955 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4956 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4957 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004958 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004959 Callee = CGF.Builder.CreateBitCast(Callee,
4960 llvm::PointerType::getUnqual(FTy));
4961 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004962}
4963
4964/// Generate code for a message send expression in the nonfragile abi.
4965CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4966 CodeGen::CodeGenFunction &CGF,
4967 QualType ResultType,
4968 Selector Sel,
4969 llvm::Value *Receiver,
4970 bool IsClassMessage,
4971 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004972 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004973 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004974 false, CallArgs);
4975}
4976
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004977llvm::GlobalVariable *
4978CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
4979 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4980
Daniel Dunbardfff2302009-03-02 05:18:14 +00004981 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004982 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4983 llvm::GlobalValue::ExternalLinkage,
4984 0, Name, &CGM.getModule());
4985 UsedGlobals.push_back(GV);
4986 }
4987
4988 return GV;
4989}
4990
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004991llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004992 const ObjCInterfaceDecl *ID,
4993 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004994
4995 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4996
4997 if (!Entry) {
4998 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004999 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005000 Entry =
5001 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5002 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005003 ClassGV,
5004 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
5005 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005006 &CGM.getModule());
5007 Entry->setAlignment(
5008 CGM.getTargetData().getPrefTypeAlignment(
5009 ObjCTypes.ClassnfABIPtrTy));
5010
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005011 if (IsSuper)
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005012 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005013 else
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005014 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005015 UsedGlobals.push_back(Entry);
5016 }
5017
5018 return Builder.CreateLoad(Entry, false, "tmp");
5019}
5020
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005021/// EmitMetaClassRef - Return a Value * of the address of _class_t
5022/// meta-data
5023///
5024llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5025 const ObjCInterfaceDecl *ID) {
5026 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5027 if (Entry)
5028 return Builder.CreateLoad(Entry, false, "tmp");
5029
5030 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
Daniel Dunbar8def7992009-03-01 04:51:18 +00005031 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005032 Entry =
5033 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5034 llvm::GlobalValue::InternalLinkage,
5035 MetaClassGV,
5036 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5037 &CGM.getModule());
5038 Entry->setAlignment(
5039 CGM.getTargetData().getPrefTypeAlignment(
5040 ObjCTypes.ClassnfABIPtrTy));
5041
5042 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
5043 UsedGlobals.push_back(Entry);
5044
5045 return Builder.CreateLoad(Entry, false, "tmp");
5046}
5047
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005048/// GetClass - Return a reference to the class for the given interface
5049/// decl.
5050llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5051 const ObjCInterfaceDecl *ID) {
5052 return EmitClassRef(Builder, ID);
5053}
5054
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005055/// Generates a message send where the super is the receiver. This is
5056/// a message send to self with special delivery semantics indicating
5057/// which class's method should be called.
5058CodeGen::RValue
5059CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5060 QualType ResultType,
5061 Selector Sel,
5062 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005063 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005064 llvm::Value *Receiver,
5065 bool IsClassMessage,
5066 const CodeGen::CallArgList &CallArgs) {
5067 // ...
5068 // Create and init a super structure; this is a (receiver, class)
5069 // pair we will pass to objc_msgSendSuper.
5070 llvm::Value *ObjCSuper =
5071 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5072
5073 llvm::Value *ReceiverAsObject =
5074 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5075 CGF.Builder.CreateStore(ReceiverAsObject,
5076 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5077
5078 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005079 llvm::Value *Target;
5080 if (IsClassMessage) {
5081 if (isCategoryImpl) {
5082 // Message sent to "super' in a class method defined in
5083 // a category implementation.
5084 Target = EmitClassRef(CGF.Builder, Class, false);
5085 Target = CGF.Builder.CreateStructGEP(Target, 0);
5086 Target = CGF.Builder.CreateLoad(Target);
5087 }
5088 else
5089 Target = EmitMetaClassRef(CGF.Builder, Class);
5090 }
5091 else
5092 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005093
5094 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5095 // and ObjCTypes types.
5096 const llvm::Type *ClassTy =
5097 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5098 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5099 CGF.Builder.CreateStore(Target,
5100 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5101
5102 return EmitMessageSend(CGF, ResultType, Sel,
5103 ObjCSuper, ObjCTypes.SuperPtrCTy,
5104 true, CallArgs);
5105}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005106
5107llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5108 Selector Sel) {
5109 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5110
5111 if (!Entry) {
5112 llvm::Constant *Casted =
5113 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5114 ObjCTypes.SelectorPtrTy);
5115 Entry =
5116 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5117 llvm::GlobalValue::InternalLinkage,
5118 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5119 &CGM.getModule());
5120 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5121 UsedGlobals.push_back(Entry);
5122 }
5123
5124 return Builder.CreateLoad(Entry, false, "tmp");
5125}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005126/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5127/// objc_assign_ivar (id src, id *dst)
5128///
5129void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5130 llvm::Value *src, llvm::Value *dst)
5131{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005132 const llvm::Type * SrcTy = src->getType();
5133 if (!isa<llvm::PointerType>(SrcTy)) {
5134 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5135 assert(Size <= 8 && "does not support size > 8");
5136 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5137 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005138 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5139 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005140 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5141 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5142 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
5143 src, dst, "assignivar");
5144 return;
5145}
5146
5147/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5148/// objc_assign_strongCast (id src, id *dst)
5149///
5150void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5151 CodeGen::CodeGenFunction &CGF,
5152 llvm::Value *src, llvm::Value *dst)
5153{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005154 const llvm::Type * SrcTy = src->getType();
5155 if (!isa<llvm::PointerType>(SrcTy)) {
5156 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5157 assert(Size <= 8 && "does not support size > 8");
5158 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5159 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005160 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5161 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005162 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5163 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5164 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
5165 src, dst, "weakassign");
5166 return;
5167}
5168
5169/// EmitObjCWeakRead - Code gen for loading value of a __weak
5170/// object: objc_read_weak (id *src)
5171///
5172llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5173 CodeGen::CodeGenFunction &CGF,
5174 llvm::Value *AddrWeakObj)
5175{
Eli Friedman8339b352009-03-07 03:57:15 +00005176 const llvm::Type* DestTy =
5177 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005178 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
5179 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
5180 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005181 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005182 return read_weak;
5183}
5184
5185/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5186/// objc_assign_weak (id src, id *dst)
5187///
5188void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5189 llvm::Value *src, llvm::Value *dst)
5190{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005191 const llvm::Type * SrcTy = src->getType();
5192 if (!isa<llvm::PointerType>(SrcTy)) {
5193 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5194 assert(Size <= 8 && "does not support size > 8");
5195 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5196 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005197 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5198 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005199 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5200 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5201 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
5202 src, dst, "weakassign");
5203 return;
5204}
5205
5206/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5207/// objc_assign_global (id src, id *dst)
5208///
5209void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5210 llvm::Value *src, llvm::Value *dst)
5211{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005212 const llvm::Type * SrcTy = src->getType();
5213 if (!isa<llvm::PointerType>(SrcTy)) {
5214 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5215 assert(Size <= 8 && "does not support size > 8");
5216 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5217 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005218 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5219 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005220 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5221 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5222 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
5223 src, dst, "globalassign");
5224 return;
5225}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005226
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005227void
5228CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5229 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005230 bool isTry = isa<ObjCAtTryStmt>(S);
5231 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5232 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005233 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005234 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005235 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005236 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5237
5238 // For @synchronized, call objc_sync_enter(sync.expr). The
5239 // evaluation of the expression must occur before we enter the
5240 // @synchronized. We can safely avoid a temp here because jumps into
5241 // @synchronized are illegal & this will dominate uses.
5242 llvm::Value *SyncArg = 0;
5243 if (!isTry) {
5244 SyncArg =
5245 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5246 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5247 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
5248 }
5249
5250 // Push an EH context entry, used for handling rethrows and jumps
5251 // through finally.
5252 CGF.PushCleanupBlock(FinallyBlock);
5253
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005254 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005255
5256 CGF.EmitBlock(TryBlock);
5257 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5258 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5259 CGF.EmitBranchThroughCleanup(FinallyEnd);
5260
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005261 // Emit the exception handler.
5262
5263 CGF.EmitBlock(TryHandler);
5264
5265 llvm::Value *llvm_eh_exception =
5266 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5267 llvm::Value *llvm_eh_selector_i64 =
5268 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5269 llvm::Value *llvm_eh_typeid_for_i64 =
5270 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5271 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5272 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5273
5274 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5275 SelectorArgs.push_back(Exc);
5276 SelectorArgs.push_back(ObjCTypes.EHPersonalityPtr);
5277
5278 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005279 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005280 bool HasCatchAll = false;
5281 if (isTry) {
5282 if (const ObjCAtCatchStmt* CatchStmt =
5283 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5284 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005285 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005286 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005287
5288 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005289 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005290 // Use i8* null here to signal this is a catch all, not a cleanup.
5291 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5292 SelectorArgs.push_back(Null);
5293 HasCatchAll = true;
5294 break;
5295 }
5296
Daniel Dunbarede8de92009-03-06 00:01:21 +00005297 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5298 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005299 llvm::Value *IDEHType =
5300 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5301 if (!IDEHType)
5302 IDEHType =
5303 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5304 llvm::GlobalValue::ExternalLinkage,
5305 0, "OBJC_EHTYPE_id", &CGM.getModule());
5306 SelectorArgs.push_back(IDEHType);
5307 HasCatchAll = true;
5308 break;
5309 }
5310
5311 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005312 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005313 assert(PT && "Invalid @catch type.");
5314 const ObjCInterfaceType *IT =
5315 PT->getPointeeType()->getAsObjCInterfaceType();
5316 assert(IT && "Invalid @catch type.");
5317 llvm::Value *EHType = GetInterfaceEHType(IT);
5318 SelectorArgs.push_back(EHType);
5319 }
5320 }
5321 }
5322
5323 // We use a cleanup unless there was already a catch all.
5324 if (!HasCatchAll) {
5325 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005326 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005327 }
5328
5329 llvm::Value *Selector =
5330 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5331 SelectorArgs.begin(), SelectorArgs.end(),
5332 "selector");
5333 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005334 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005335 const Stmt *CatchBody = Handlers[i].second;
5336
5337 llvm::BasicBlock *Next = 0;
5338
5339 // The last handler always matches.
5340 if (i + 1 != e) {
5341 assert(CatchParam && "Only last handler can be a catch all.");
5342
5343 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5344 Next = CGF.createBasicBlock("catch.next");
5345 llvm::Value *Id =
5346 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5347 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5348 ObjCTypes.Int8PtrTy));
5349 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5350 Match, Next);
5351
5352 CGF.EmitBlock(Match);
5353 }
5354
5355 if (CatchBody) {
5356 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5357 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5358
5359 // Cleanups must call objc_end_catch.
5360 //
5361 // FIXME: It seems incorrect for objc_begin_catch to be inside
5362 // this context, but this matches gcc.
5363 CGF.PushCleanupBlock(MatchEnd);
5364 CGF.setInvokeDest(MatchHandler);
5365
5366 llvm::Value *ExcObject =
5367 CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc);
5368
5369 // Bind the catch parameter if it exists.
5370 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005371 ExcObject =
5372 CGF.Builder.CreateBitCast(ExcObject,
5373 CGF.ConvertType(CatchParam->getType()));
5374 // CatchParam is a ParmVarDecl because of the grammar
5375 // construction used to handle this, but for codegen purposes
5376 // we treat this as a local decl.
5377 CGF.EmitLocalBlockVarDecl(*CatchParam);
5378 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005379 }
5380
5381 CGF.ObjCEHValueStack.push_back(ExcObject);
5382 CGF.EmitStmt(CatchBody);
5383 CGF.ObjCEHValueStack.pop_back();
5384
5385 CGF.EmitBranchThroughCleanup(FinallyEnd);
5386
5387 CGF.EmitBlock(MatchHandler);
5388
5389 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5390 // We are required to emit this call to satisfy LLVM, even
5391 // though we don't use the result.
5392 llvm::SmallVector<llvm::Value*, 8> Args;
5393 Args.push_back(Exc);
5394 Args.push_back(ObjCTypes.EHPersonalityPtr);
5395 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5396 0));
5397 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5398 CGF.Builder.CreateStore(Exc, RethrowPtr);
5399 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5400
5401 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5402
5403 CGF.EmitBlock(MatchEnd);
5404
5405 // Unfortunately, we also have to generate another EH frame here
5406 // in case this throws.
5407 llvm::BasicBlock *MatchEndHandler =
5408 CGF.createBasicBlock("match.end.handler");
5409 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5410 CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn,
5411 Cont, MatchEndHandler,
5412 Args.begin(), Args.begin());
5413
5414 CGF.EmitBlock(Cont);
5415 if (Info.SwitchBlock)
5416 CGF.EmitBlock(Info.SwitchBlock);
5417 if (Info.EndBlock)
5418 CGF.EmitBlock(Info.EndBlock);
5419
5420 CGF.EmitBlock(MatchEndHandler);
5421 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5422 // We are required to emit this call to satisfy LLVM, even
5423 // though we don't use the result.
5424 Args.clear();
5425 Args.push_back(Exc);
5426 Args.push_back(ObjCTypes.EHPersonalityPtr);
5427 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5428 0));
5429 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5430 CGF.Builder.CreateStore(Exc, RethrowPtr);
5431 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5432
5433 if (Next)
5434 CGF.EmitBlock(Next);
5435 } else {
5436 assert(!Next && "catchup should be last handler.");
5437
5438 CGF.Builder.CreateStore(Exc, RethrowPtr);
5439 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5440 }
5441 }
5442
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005443 // Pop the cleanup entry, the @finally is outside this cleanup
5444 // scope.
5445 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5446 CGF.setInvokeDest(PrevLandingPad);
5447
5448 CGF.EmitBlock(FinallyBlock);
5449
5450 if (isTry) {
5451 if (const ObjCAtFinallyStmt* FinallyStmt =
5452 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5453 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5454 } else {
5455 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5456 // @synchronized.
5457 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005458 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005459
5460 if (Info.SwitchBlock)
5461 CGF.EmitBlock(Info.SwitchBlock);
5462 if (Info.EndBlock)
5463 CGF.EmitBlock(Info.EndBlock);
5464
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005465 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005466 CGF.EmitBranch(FinallyEnd);
5467
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005468 CGF.EmitBlock(FinallyRethrow);
5469 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn,
5470 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005471 CGF.Builder.CreateUnreachable();
5472
5473 CGF.EmitBlock(FinallyEnd);
5474}
5475
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005476/// EmitThrowStmt - Generate code for a throw statement.
5477void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5478 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005479 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005480 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005481 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005482 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005483 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5484 "Unexpected rethrow outside @catch block.");
5485 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005486 }
5487
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005488 llvm::Value *ExceptionAsObject =
5489 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5490 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5491 if (InvokeDest) {
5492 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5493 CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn,
5494 Cont, InvokeDest,
5495 &ExceptionAsObject, &ExceptionAsObject + 1);
5496 CGF.EmitBlock(Cont);
5497 } else
5498 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
5499 CGF.Builder.CreateUnreachable();
5500
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005501 // Clear the insertion point to indicate we are in unreachable code.
5502 CGF.Builder.ClearInsertionPoint();
5503}
Daniel Dunbare588b992009-03-01 04:46:24 +00005504
5505llvm::Value *
5506CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceType *IT) {
5507 const ObjCInterfaceDecl *ID = IT->getDecl();
5508 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
5509 if (Entry)
5510 return Entry;
5511
5512 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
5513 std::string VTableName = "objc_ehtype_vtable";
5514 llvm::GlobalVariable *VTableGV =
5515 CGM.getModule().getGlobalVariable(VTableName);
5516 if (!VTableGV)
5517 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5518 llvm::GlobalValue::ExternalLinkage,
5519 0, VTableName, &CGM.getModule());
5520
5521 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5522
5523 std::vector<llvm::Constant*> Values(3);
5524 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5525 Values[1] = GetClassName(ID->getIdentifier());
5526 Values[2] = GetClassGlobal(ClassName);
5527 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5528
5529 Entry =
5530 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00005531 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbare588b992009-03-01 04:46:24 +00005532 Init,
5533 (std::string("OBJC_EHTYPE_$_") +
5534 ID->getIdentifier()->getName()),
5535 &CGM.getModule());
5536
5537 return Entry;
5538}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005539
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005540/* *** */
5541
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005542CodeGen::CGObjCRuntime *
5543CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005544 return new CGObjCMac(CGM);
5545}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005546
5547CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005548CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005549 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005550}