blob: 129472b0025bdb1ab5ac13b0accf956759a5bc5a [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 Lattnerb02e53b2009-04-06 16:53:45 +0000110 llvm::Constant *getSyncEnterFn() {
111 // void objc_sync_enter (id)
112 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
113 llvm::FunctionType *FTy =
114 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
115 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
116 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000117
118 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000119 llvm::Constant *SyncExitFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000120
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000121 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
122 ~ObjCCommonTypesHelper(){}
123};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000124
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000125/// ObjCTypesHelper - Helper class that encapsulates lazy
126/// construction of varies types used during ObjC generation.
127class ObjCTypesHelper : public ObjCCommonTypesHelper {
128private:
129
Chris Lattner74391b42009-03-22 21:03:39 +0000130 llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
131 llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn,
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000132 *MessageSendSuperFpretFn;
133
134public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000135 /// SymtabTy - LLVM type for struct objc_symtab.
136 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000137 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
138 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000139 /// ModuleTy - LLVM type for struct objc_module.
140 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000141
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000142 /// ProtocolTy - LLVM type for struct objc_protocol.
143 const llvm::StructType *ProtocolTy;
144 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
145 const llvm::Type *ProtocolPtrTy;
146 /// ProtocolExtensionTy - LLVM type for struct
147 /// objc_protocol_extension.
148 const llvm::StructType *ProtocolExtensionTy;
149 /// ProtocolExtensionTy - LLVM type for struct
150 /// objc_protocol_extension *.
151 const llvm::Type *ProtocolExtensionPtrTy;
152 /// MethodDescriptionTy - LLVM type for struct
153 /// objc_method_description.
154 const llvm::StructType *MethodDescriptionTy;
155 /// MethodDescriptionListTy - LLVM type for struct
156 /// objc_method_description_list.
157 const llvm::StructType *MethodDescriptionListTy;
158 /// MethodDescriptionListPtrTy - LLVM type for struct
159 /// objc_method_description_list *.
160 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000161 /// ProtocolListTy - LLVM type for struct objc_property_list.
162 const llvm::Type *ProtocolListTy;
163 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
164 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000165 /// CategoryTy - LLVM type for struct objc_category.
166 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000167 /// ClassTy - LLVM type for struct objc_class.
168 const llvm::StructType *ClassTy;
169 /// ClassPtrTy - LLVM type for struct objc_class *.
170 const llvm::Type *ClassPtrTy;
171 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
172 const llvm::StructType *ClassExtensionTy;
173 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
174 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000175 // IvarTy - LLVM type for struct objc_ivar.
176 const llvm::StructType *IvarTy;
177 /// IvarListTy - LLVM type for struct objc_ivar_list.
178 const llvm::Type *IvarListTy;
179 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
180 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000181 /// MethodListTy - LLVM type for struct objc_method_list.
182 const llvm::Type *MethodListTy;
183 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
184 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000185
186 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
187 const llvm::Type *ExceptionDataTy;
188
Anders Carlsson124526b2008-09-09 10:10:21 +0000189 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner74391b42009-03-22 21:03:39 +0000190 llvm::Constant *ExceptionTryEnterFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000191
192 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000193 llvm::Constant *ExceptionTryExitFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000194
195 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner74391b42009-03-22 21:03:39 +0000196 llvm::Constant *ExceptionExtractFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000197
198 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner74391b42009-03-22 21:03:39 +0000199 llvm::Constant *ExceptionMatchFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000200
201 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner74391b42009-03-22 21:03:39 +0000202 llvm::Constant *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000203
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000204public:
205 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000206 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000207
208
Chris Lattner74391b42009-03-22 21:03:39 +0000209 llvm::Constant *getSendFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000210 return IsSuper ? MessageSendSuperFn : MessageSendFn;
211 }
212
Chris Lattner74391b42009-03-22 21:03:39 +0000213 llvm::Constant *getSendStretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000214 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
215 }
216
Chris Lattner74391b42009-03-22 21:03:39 +0000217 llvm::Constant *getSendFpretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000218 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
219 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000220};
221
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000223/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000224class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000225public:
Chris Lattner74391b42009-03-22 21:03:39 +0000226 llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000227 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
228 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
229 *MessageSendSuper2StretFixupFn;
230
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000231 // MethodListnfABITy - LLVM for struct _method_list_t
232 const llvm::StructType *MethodListnfABITy;
233
234 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
235 const llvm::Type *MethodListnfABIPtrTy;
236
237 // ProtocolnfABITy = LLVM for struct _protocol_t
238 const llvm::StructType *ProtocolnfABITy;
239
Daniel Dunbar948e2582009-02-15 07:36:20 +0000240 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
241 const llvm::Type *ProtocolnfABIPtrTy;
242
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000243 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
244 const llvm::StructType *ProtocolListnfABITy;
245
246 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
247 const llvm::Type *ProtocolListnfABIPtrTy;
248
249 // ClassnfABITy - LLVM for struct _class_t
250 const llvm::StructType *ClassnfABITy;
251
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000252 // ClassnfABIPtrTy - LLVM for struct _class_t*
253 const llvm::Type *ClassnfABIPtrTy;
254
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000255 // IvarnfABITy - LLVM for struct _ivar_t
256 const llvm::StructType *IvarnfABITy;
257
258 // IvarListnfABITy - LLVM for struct _ivar_list_t
259 const llvm::StructType *IvarListnfABITy;
260
261 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
262 const llvm::Type *IvarListnfABIPtrTy;
263
264 // ClassRonfABITy - LLVM for struct _class_ro_t
265 const llvm::StructType *ClassRonfABITy;
266
267 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
268 const llvm::Type *ImpnfABITy;
269
270 // CategorynfABITy - LLVM for struct _category_t
271 const llvm::StructType *CategorynfABITy;
272
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000273 // New types for nonfragile abi messaging.
274
275 // MessageRefTy - LLVM for:
276 // struct _message_ref_t {
277 // IMP messenger;
278 // SEL name;
279 // };
280 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000281 // MessageRefCTy - clang type for struct _message_ref_t
282 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000283
284 // MessageRefPtrTy - LLVM for struct _message_ref_t*
285 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000286 // MessageRefCPtrTy - clang type for struct _message_ref_t*
287 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000288
Fariborz Jahanianef163782009-02-05 01:13:09 +0000289 // MessengerTy - Type of the messenger (shown as IMP above)
290 const llvm::FunctionType *MessengerTy;
291
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000292 // SuperMessageRefTy - LLVM for:
293 // struct _super_message_ref_t {
294 // SUPER_IMP messenger;
295 // SEL name;
296 // };
297 const llvm::StructType *SuperMessageRefTy;
298
299 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
300 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000301
302 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
303 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000304 llvm::Value *getEHPersonalityPtr() {
305 llvm::Constant *Personality =
306 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
307 std::vector<const llvm::Type*>(),
308 true),
309 "__objc_personality_v0");
310 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
311 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000312
Chris Lattner74391b42009-03-22 21:03:39 +0000313 llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn;
Daniel Dunbare588b992009-03-01 04:46:24 +0000314
315 const llvm::StructType *EHTypeTy;
316 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000317
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000318 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
319 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000320};
321
322class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000323public:
324 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000325 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000326 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000327 unsigned int ivar_bytepos;
328 unsigned int ivar_size;
329 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
330 };
331
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000332 class SKIP_SCAN {
333 public:
334 unsigned int skip;
335 unsigned int scan;
336 SKIP_SCAN() : skip(0), scan(0) {}
337 };
338
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000339protected:
340 CodeGen::CodeGenModule &CGM;
341 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000342 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000343
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000344 // gc ivar layout bitmap calculation helper caches.
345 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
346 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
347 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000348
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000349 /// LazySymbols - Symbols to generate a lazy reference for. See
350 /// DefinedSymbols and FinishModule().
351 std::set<IdentifierInfo*> LazySymbols;
352
353 /// DefinedSymbols - External symbols which are defined by this
354 /// module. The symbols in this list and LazySymbols are used to add
355 /// special linker symbols which ensure that Objective-C modules are
356 /// linked properly.
357 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000358
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000359 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000360 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000361
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000362 /// MethodVarNames - uniqued method variable names.
363 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000364
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000365 /// MethodVarTypes - uniqued method type signatures. We have to use
366 /// a StringMap here because have no other unique reference.
367 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000368
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000369 /// MethodDefinitions - map of methods which have been defined in
370 /// this translation unit.
371 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000372
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000373 /// PropertyNames - uniqued method variable names.
374 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000375
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000376 /// ClassReferences - uniqued class references.
377 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000378
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000379 /// SelectorReferences - uniqued selector references.
380 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000381
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000382 /// Protocols - Protocols for which an objc_protocol structure has
383 /// been emitted. Forward declarations are handled by creating an
384 /// empty structure whose initializer is filled in when/if defined.
385 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000386
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000387 /// DefinedProtocols - Protocols which have actually been
388 /// defined. We should not need this, see FIXME in GenerateProtocol.
389 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000390
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000391 /// DefinedClasses - List of defined classes.
392 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000393
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000394 /// DefinedCategories - List of defined categories.
395 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000396
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000397 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000398 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000399 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000400
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000401 /// GetNameForMethod - Return a name for the given method.
402 /// \param[out] NameOut - The return value.
403 void GetNameForMethod(const ObjCMethodDecl *OMD,
404 const ObjCContainerDecl *CD,
405 std::string &NameOut);
406
407 /// GetMethodVarName - Return a unique constant for the given
408 /// selector's name. The return value has type char *.
409 llvm::Constant *GetMethodVarName(Selector Sel);
410 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
411 llvm::Constant *GetMethodVarName(const std::string &Name);
412
413 /// GetMethodVarType - Return a unique constant for the given
414 /// selector's name. The return value has type char *.
415
416 // FIXME: This is a horrible name.
417 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Devang Patel7794bb82009-03-04 18:21:39 +0000418 llvm::Constant *GetMethodVarType(FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000419
420 /// GetPropertyName - Return a unique constant for the given
421 /// name. The return value has type char *.
422 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
423
424 // FIXME: This can be dropped once string functions are unified.
425 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
426 const Decl *Container);
427
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000428 /// GetClassName - Return a unique constant for the given selector's
429 /// name. The return value has type char *.
430 llvm::Constant *GetClassName(IdentifierInfo *Ident);
431
Fariborz Jahanian21e6f172009-03-11 21:42:00 +0000432 /// GetInterfaceDeclStructLayout - Get layout for ivars of given
433 /// interface declaration.
434 const llvm::StructLayout *GetInterfaceDeclStructLayout(
435 const ObjCInterfaceDecl *ID) const;
436
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000437 /// BuildIvarLayout - Builds ivar layout bitmap for the class
438 /// implementation for the __strong or __weak case.
439 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000440 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
441 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000442
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000443 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
444 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000445 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000446 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000447 unsigned int BytePos, bool ForStrongLayout,
448 int &Index, int &SkIndex, bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000449
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000450 /// GetIvarLayoutName - Returns a unique constant for the given
451 /// ivar layout bitmap.
452 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
453 const ObjCCommonTypesHelper &ObjCTypes);
454
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000455 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
456 RecordDecl::field_iterator &FIV,
457 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000458 /// EmitPropertyList - Emit the given property list. The return
459 /// value has type PropertyListPtrTy.
460 llvm::Constant *EmitPropertyList(const std::string &Name,
461 const Decl *Container,
462 const ObjCContainerDecl *OCD,
463 const ObjCCommonTypesHelper &ObjCTypes);
464
Fariborz Jahanianda320092009-01-29 19:24:30 +0000465 /// GetProtocolRef - Return a reference to the internal protocol
466 /// description, creating an empty one if it has not been
467 /// defined. The return value has type ProtocolPtrTy.
468 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000469
470 /// GetIvarBaseOffset - returns ivars byte offset.
471 uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000472 const FieldDecl *Field);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000473
Chris Lattnercd0ee142009-03-31 08:33:16 +0000474 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000475 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
476 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000477 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000478
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000479 /// CreateMetadataVar - Create a global variable with internal
480 /// linkage for use by the Objective-C runtime.
481 ///
482 /// This is a convenience wrapper which not only creates the
483 /// variable, but also sets the section and alignment and adds the
484 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000485 ///
486 /// \param Name - The variable name.
487 /// \param Init - The variable initializer; this is also used to
488 /// define the type of the variable.
489 /// \param Section - The section the variable should go into, or 0.
490 /// \param Align - The alignment for the variable, or 0.
491 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000492 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000493 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
494 llvm::Constant *Init,
495 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000496 unsigned Align,
497 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000498
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000499public:
500 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
501 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000502
Steve Naroff33fdb732009-03-31 16:53:37 +0000503 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000504
505 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
506 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000507
508 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
509
510 /// GetOrEmitProtocol - Get the protocol object for the given
511 /// declaration, emitting it if necessary. The return value has type
512 /// ProtocolPtrTy.
513 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
514
515 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
516 /// object for the given declaration, emitting it if needed. These
517 /// forward references will be filled in with empty bodies if no
518 /// definition is seen. The return value has type ProtocolPtrTy.
519 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000520};
521
522class CGObjCMac : public CGObjCCommonMac {
523private:
524 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000525 /// EmitImageInfo - Emit the image info marker used to encode some module
526 /// level information.
527 void EmitImageInfo();
528
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000529 /// EmitModuleInfo - Another marker encoding module level
530 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000531 void EmitModuleInfo();
532
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 /// EmitModuleSymols - Emit module symbols, the list of defined
534 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000535 llvm::Constant *EmitModuleSymbols();
536
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000537 /// FinishModule - Write out global data structures at the end of
538 /// processing a translation unit.
539 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000540
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// EmitClassExtension - Generate the class extension structure used
542 /// to store the weak ivar layout and properties. The return value
543 /// has type ClassExtensionPtrTy.
544 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
545
546 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
547 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000548 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000549 const ObjCInterfaceDecl *ID);
550
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000551 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000552 QualType ResultType,
553 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000554 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000555 QualType Arg0Ty,
556 bool IsSuper,
557 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000558
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000559 /// EmitIvarList - Emit the ivar list for the given
560 /// implementation. If ForClass is true the list of class ivars
561 /// (i.e. metaclass ivars) is emitted, otherwise the list of
562 /// interface ivars will be emitted. The return value has type
563 /// IvarListPtrTy.
564 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000565 bool ForClass);
566
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000567 /// EmitMetaClass - Emit a forward reference to the class structure
568 /// for the metaclass of the given interface. The return value has
569 /// type ClassPtrTy.
570 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
571
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000572 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000573 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000574 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
575 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000576 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000577 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000578
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000579 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000580
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000581 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000582
583 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000584 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000585 llvm::Constant *EmitMethodList(const std::string &Name,
586 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000587 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000588
589 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000590 /// method declarations.
591 /// - TypeName: The name for the type containing the methods.
592 /// - IsProtocol: True iff these methods are for a protocol.
593 /// - ClassMethds: True iff these are class methods.
594 /// - Required: When true, only "required" methods are
595 /// listed. Similarly, when false only "optional" methods are
596 /// listed. For classes this should always be true.
597 /// - begin, end: The method list to output.
598 ///
599 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000600 llvm::Constant *EmitMethodDescList(const std::string &Name,
601 const char *Section,
602 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000603
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000604 /// GetOrEmitProtocol - Get the protocol object for the given
605 /// declaration, emitting it if necessary. The return value has type
606 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000607 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000608
609 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
610 /// object for the given declaration, emitting it if needed. These
611 /// forward references will be filled in with empty bodies if no
612 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000613 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000614
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000615 /// EmitProtocolExtension - Generate the protocol extension
616 /// structure used to store optional instance and class methods, and
617 /// protocol properties. The return value has type
618 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000619 llvm::Constant *
620 EmitProtocolExtension(const ObjCProtocolDecl *PD,
621 const ConstantVector &OptInstanceMethods,
622 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000623
624 /// EmitProtocolList - Generate the list of referenced
625 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000626 llvm::Constant *EmitProtocolList(const std::string &Name,
627 ObjCProtocolDecl::protocol_iterator begin,
628 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000629
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000630 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
631 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000632 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000633
Fariborz Jahanianda320092009-01-29 19:24:30 +0000634 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000635 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000636
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000637 virtual llvm::Function *ModuleInitFunction();
638
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000639 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000640 QualType ResultType,
641 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000642 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000643 bool IsClassMessage,
644 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000645
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000646 virtual CodeGen::RValue
647 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000648 QualType ResultType,
649 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000650 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000651 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000652 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000653 bool IsClassMessage,
654 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000655
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000656 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000657 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000658
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000659 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000660
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000661 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000662
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000663 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000664
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000665 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000666 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000667
Chris Lattner74391b42009-03-22 21:03:39 +0000668 virtual llvm::Constant *GetPropertyGetFunction();
669 virtual llvm::Constant *GetPropertySetFunction();
670 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000671
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000672 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
673 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000674 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
675 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000676 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000677 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000678 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
679 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000680 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
681 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000682 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
683 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000684 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
685 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000686
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000687 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
688 QualType ObjectTy,
689 llvm::Value *BaseValue,
690 const ObjCIvarDecl *Ivar,
691 const FieldDecl *Field,
692 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000693 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
694 ObjCInterfaceDecl *Interface,
695 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000696};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000697
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000698class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000699private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000700 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000701 llvm::GlobalVariable* ObjCEmptyCacheVar;
702 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000703
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000704 /// MetaClassReferences - uniqued meta class references.
705 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +0000706
707 /// EHTypeReferences - uniqued class ehtype references.
708 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000709
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000710 /// FinishNonFragileABIModule - Write out global data structures at the end of
711 /// processing a translation unit.
712 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000713
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000714 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
715 unsigned InstanceStart,
716 unsigned InstanceSize,
717 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000718 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
719 llvm::Constant *IsAGV,
720 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000721 llvm::Constant *ClassRoGV,
722 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000723
724 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
725
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000726 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
727
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000728 /// EmitMethodList - Emit the method list for the given
729 /// implementation. The return value has type MethodListnfABITy.
730 llvm::Constant *EmitMethodList(const std::string &Name,
731 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000732 const ConstantVector &Methods);
733 /// EmitIvarList - Emit the ivar list for the given
734 /// implementation. If ForClass is true the list of class ivars
735 /// (i.e. metaclass ivars) is emitted, otherwise the list of
736 /// interface ivars will be emitted. The return value has type
737 /// IvarListnfABIPtrTy.
738 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000739
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000740 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000741 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000742 unsigned long int offset);
743
Fariborz Jahanianda320092009-01-29 19:24:30 +0000744 /// GetOrEmitProtocol - Get the protocol object for the given
745 /// declaration, emitting it if necessary. The return value has type
746 /// ProtocolPtrTy.
747 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
748
749 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
750 /// object for the given declaration, emitting it if needed. These
751 /// forward references will be filled in with empty bodies if no
752 /// definition is seen. The return value has type ProtocolPtrTy.
753 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
754
755 /// EmitProtocolList - Generate the list of referenced
756 /// protocols. The return value has type ProtocolListPtrTy.
757 llvm::Constant *EmitProtocolList(const std::string &Name,
758 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000759 ObjCProtocolDecl::protocol_iterator end);
760
761 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
762 QualType ResultType,
763 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000764 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000765 QualType Arg0Ty,
766 bool IsSuper,
767 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +0000768
769 /// GetClassGlobal - Return the global variable for the Objective-C
770 /// class of the given name.
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000771 llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
772 bool AddToUsed = true);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000773
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000774 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
775 /// for the given class.
776 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000777 const ObjCInterfaceDecl *ID,
778 bool IsSuper = false);
779
780 /// EmitMetaClassRef - Return a Value * of the address of _class_t
781 /// meta-data
782 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
783 const ObjCInterfaceDecl *ID);
784
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000785 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
786 /// the given ivar.
787 ///
788 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000789 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000790 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000791
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000792 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
793 /// for the given selector.
794 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +0000795
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000796 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +0000797 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000798 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
799 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000800
801 const char *getMetaclassSymbolPrefix() const {
802 return "OBJC_METACLASS_$_";
803 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000804
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000805 const char *getClassSymbolPrefix() const {
806 return "OBJC_CLASS_$_";
807 }
808
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000809public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000810 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000811 // FIXME. All stubs for now!
812 virtual llvm::Function *ModuleInitFunction();
813
814 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
815 QualType ResultType,
816 Selector Sel,
817 llvm::Value *Receiver,
818 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000819 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000820
821 virtual CodeGen::RValue
822 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
823 QualType ResultType,
824 Selector Sel,
825 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000826 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000827 llvm::Value *Receiver,
828 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000829 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000830
831 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000832 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000833
834 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000835 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000836
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000837 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000838
839 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000840 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000841 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000842
Chris Lattner74391b42009-03-22 21:03:39 +0000843 virtual llvm::Constant *GetPropertyGetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000844 return ObjCTypes.GetPropertyFn;
845 }
Chris Lattner74391b42009-03-22 21:03:39 +0000846 virtual llvm::Constant *GetPropertySetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000847 return ObjCTypes.SetPropertyFn;
848 }
Chris Lattner74391b42009-03-22 21:03:39 +0000849 virtual llvm::Constant *EnumerationMutationFunction() {
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000850 return ObjCTypes.EnumerationMutationFn;
851 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000852
853 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000854 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000855 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000856 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000857 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000858 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000859 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000860 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000861 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000862 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000863 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000864 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000865 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000866 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000867 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
868 QualType ObjectTy,
869 llvm::Value *BaseValue,
870 const ObjCIvarDecl *Ivar,
871 const FieldDecl *Field,
872 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000873 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
874 ObjCInterfaceDecl *Interface,
875 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000876};
877
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000878} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000879
880/* *** Helper Functions *** */
881
882/// getConstantGEP() - Help routine to construct simple GEPs.
883static llvm::Constant *getConstantGEP(llvm::Constant *C,
884 unsigned idx0,
885 unsigned idx1) {
886 llvm::Value *Idxs[] = {
887 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
888 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
889 };
890 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
891}
892
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000893/// hasObjCExceptionAttribute - Return true if this class or any super
894/// class has the __objc_exception__ attribute.
895static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +0000896 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000897 return true;
898 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
899 return hasObjCExceptionAttribute(Super);
900 return false;
901}
902
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000903/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000904
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000905CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
906 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000907{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000908 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000909 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000910}
911
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000912/// GetClass - Return a reference to the class for the given interface
913/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000914llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000915 const ObjCInterfaceDecl *ID) {
916 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000917}
918
919/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000920llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000921 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000922}
923
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000924/// Generate a constant CFString object.
925/*
926 struct __builtin_CFString {
927 const int *isa; // point to __CFConstantStringClassReference
928 int flags;
929 const char *str;
930 long length;
931 };
932*/
933
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000934llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +0000935 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +0000936 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000937}
938
939/// Generates a message send where the super is the receiver. This is
940/// a message send to self with special delivery semantics indicating
941/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000942CodeGen::RValue
943CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000944 QualType ResultType,
945 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000946 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000947 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000948 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000949 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000950 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000951 // Create and init a super structure; this is a (receiver, class)
952 // pair we will pass to objc_msgSendSuper.
953 llvm::Value *ObjCSuper =
954 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
955 llvm::Value *ReceiverAsObject =
956 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
957 CGF.Builder.CreateStore(ReceiverAsObject,
958 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000959
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000960 // If this is a class message the metaclass is passed as the target.
961 llvm::Value *Target;
962 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000963 if (isCategoryImpl) {
964 // Message sent to 'super' in a class method defined in a category
965 // implementation requires an odd treatment.
966 // If we are in a class method, we must retrieve the
967 // _metaclass_ for the current class, pointed at by
968 // the class's "isa" pointer. The following assumes that
969 // isa" is the first ivar in a class (which it must be).
970 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
971 Target = CGF.Builder.CreateStructGEP(Target, 0);
972 Target = CGF.Builder.CreateLoad(Target);
973 }
974 else {
975 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
976 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
977 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
978 Target = Super;
979 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000980 } else {
981 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
982 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000983 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
984 // and ObjCTypes types.
985 const llvm::Type *ClassTy =
986 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000987 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000988 CGF.Builder.CreateStore(Target,
989 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
990
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000991 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000992 ObjCSuper, ObjCTypes.SuperPtrCTy,
993 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000994}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000995
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000996/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000997CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000998 QualType ResultType,
999 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001000 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001001 bool IsClassMessage,
1002 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001003 llvm::Value *Arg0 =
1004 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001005 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001006 Arg0, CGF.getContext().getObjCIdType(),
1007 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001008}
1009
1010CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001011 QualType ResultType,
1012 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001013 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001014 QualType Arg0Ty,
1015 bool IsSuper,
1016 const CallArgList &CallArgs) {
1017 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001018 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1019 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1020 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001021 CGF.getContext().getObjCSelType()));
1022 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001023
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001024 CodeGenTypes &Types = CGM.getTypes();
1025 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
1026 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001027
1028 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001029 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001030 Fn = ObjCTypes.getSendStretFn(IsSuper);
1031 } else if (ResultType->isFloatingType()) {
1032 // FIXME: Sadly, this is wrong. This actually depends on the
1033 // architecture. This happens to be right for x86-32 though.
1034 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1035 } else {
1036 Fn = ObjCTypes.getSendFn(IsSuper);
1037 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001038 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001039 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001040}
1041
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001042llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001043 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001044 // FIXME: I don't understand why gcc generates this, or where it is
1045 // resolved. Investigate. Its also wasteful to look this up over and
1046 // over.
1047 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1048
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001049 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1050 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001051}
1052
Fariborz Jahanianda320092009-01-29 19:24:30 +00001053void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001054 // FIXME: We shouldn't need this, the protocol decl should contain
1055 // enough information to tell us whether this was a declaration or a
1056 // definition.
1057 DefinedProtocols.insert(PD->getIdentifier());
1058
1059 // If we have generated a forward reference to this protocol, emit
1060 // it now. Otherwise do nothing, the protocol objects are lazily
1061 // emitted.
1062 if (Protocols.count(PD->getIdentifier()))
1063 GetOrEmitProtocol(PD);
1064}
1065
Fariborz Jahanianda320092009-01-29 19:24:30 +00001066llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001067 if (DefinedProtocols.count(PD->getIdentifier()))
1068 return GetOrEmitProtocol(PD);
1069 return GetOrEmitProtocolRef(PD);
1070}
1071
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072/*
1073 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1074 struct _objc_protocol {
1075 struct _objc_protocol_extension *isa;
1076 char *protocol_name;
1077 struct _objc_protocol_list *protocol_list;
1078 struct _objc__method_prototype_list *instance_methods;
1079 struct _objc__method_prototype_list *class_methods
1080 };
1081
1082 See EmitProtocolExtension().
1083*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001084llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1085 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1086
1087 // Early exit if a defining object has already been generated.
1088 if (Entry && Entry->hasInitializer())
1089 return Entry;
1090
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001091 // FIXME: I don't understand why gcc generates this, or where it is
1092 // resolved. Investigate. Its also wasteful to look this up over and
1093 // over.
1094 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1095
Chris Lattner8ec03f52008-11-24 03:54:41 +00001096 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001097
1098 // Construct method lists.
1099 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1100 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001101 for (ObjCProtocolDecl::instmeth_iterator
1102 i = PD->instmeth_begin(CGM.getContext()),
1103 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001104 ObjCMethodDecl *MD = *i;
1105 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1106 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1107 OptInstanceMethods.push_back(C);
1108 } else {
1109 InstanceMethods.push_back(C);
1110 }
1111 }
1112
Douglas Gregor6ab35242009-04-09 21:40:53 +00001113 for (ObjCProtocolDecl::classmeth_iterator
1114 i = PD->classmeth_begin(CGM.getContext()),
1115 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001116 ObjCMethodDecl *MD = *i;
1117 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1118 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1119 OptClassMethods.push_back(C);
1120 } else {
1121 ClassMethods.push_back(C);
1122 }
1123 }
1124
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001125 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001126 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001127 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001128 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001129 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001130 PD->protocol_begin(),
1131 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001132 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001133 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1134 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001135 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1136 InstanceMethods);
1137 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001138 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1139 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001140 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1141 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001142 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1143 Values);
1144
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001145 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001146 // Already created, fix the linkage and update the initializer.
1147 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001148 Entry->setInitializer(Init);
1149 } else {
1150 Entry =
1151 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1152 llvm::GlobalValue::InternalLinkage,
1153 Init,
1154 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1155 &CGM.getModule());
1156 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001157 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001158 UsedGlobals.push_back(Entry);
1159 // FIXME: Is this necessary? Why only for protocol?
1160 Entry->setAlignment(4);
1161 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001162
1163 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001164}
1165
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001166llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001167 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1168
1169 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001170 // We use the initializer as a marker of whether this is a forward
1171 // reference or not. At module finalization we add the empty
1172 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001173 Entry =
1174 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001175 llvm::GlobalValue::ExternalLinkage,
1176 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001177 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001178 &CGM.getModule());
1179 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001180 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001181 UsedGlobals.push_back(Entry);
1182 // FIXME: Is this necessary? Why only for protocol?
1183 Entry->setAlignment(4);
1184 }
1185
1186 return Entry;
1187}
1188
1189/*
1190 struct _objc_protocol_extension {
1191 uint32_t size;
1192 struct objc_method_description_list *optional_instance_methods;
1193 struct objc_method_description_list *optional_class_methods;
1194 struct objc_property_list *instance_properties;
1195 };
1196*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001197llvm::Constant *
1198CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1199 const ConstantVector &OptInstanceMethods,
1200 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001201 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001202 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001203 std::vector<llvm::Constant*> Values(4);
1204 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001205 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001206 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1207 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001208 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1209 OptInstanceMethods);
1210 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001211 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1212 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001213 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1214 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001215 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1216 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001217 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001218
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001219 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001220 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1221 Values[3]->isNullValue())
1222 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1223
1224 llvm::Constant *Init =
1225 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001226
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001227 // No special section, but goes in llvm.used
1228 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1229 Init,
1230 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001231}
1232
1233/*
1234 struct objc_protocol_list {
1235 struct objc_protocol_list *next;
1236 long count;
1237 Protocol *list[];
1238 };
1239*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001240llvm::Constant *
1241CGObjCMac::EmitProtocolList(const std::string &Name,
1242 ObjCProtocolDecl::protocol_iterator begin,
1243 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001244 std::vector<llvm::Constant*> ProtocolRefs;
1245
Daniel Dunbardbc933702008-08-21 21:57:41 +00001246 for (; begin != end; ++begin)
1247 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001248
1249 // Just return null for empty protocol lists
1250 if (ProtocolRefs.empty())
1251 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1252
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001253 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001254 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1255
1256 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001257 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001258 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1259 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1260 Values[2] =
1261 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1262 ProtocolRefs.size()),
1263 ProtocolRefs);
1264
1265 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1266 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001267 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001268 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001269 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1270}
1271
1272/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001273 struct _objc_property {
1274 const char * const name;
1275 const char * const attributes;
1276 };
1277
1278 struct _objc_property_list {
1279 uint32_t entsize; // sizeof (struct _objc_property)
1280 uint32_t prop_count;
1281 struct _objc_property[prop_count];
1282 };
1283*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001284llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1285 const Decl *Container,
1286 const ObjCContainerDecl *OCD,
1287 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001288 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001289 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1290 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001291 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001292 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001293 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001294 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1295 Prop));
1296 }
1297
1298 // Return null for empty list.
1299 if (Properties.empty())
1300 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1301
1302 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001303 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001304 std::vector<llvm::Constant*> Values(3);
1305 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1306 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1307 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1308 Properties.size());
1309 Values[2] = llvm::ConstantArray::get(AT, Properties);
1310 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1311
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001312 // No special section on property lists?
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001313 llvm::GlobalVariable *GV =
1314 CreateMetadataVar(Name, Init, (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
1315 0, true);
1316 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001317}
1318
1319/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001320 struct objc_method_description_list {
1321 int count;
1322 struct objc_method_description list[];
1323 };
1324*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001325llvm::Constant *
1326CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1327 std::vector<llvm::Constant*> Desc(2);
1328 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1329 ObjCTypes.SelectorPtrTy);
1330 Desc[1] = GetMethodVarType(MD);
1331 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1332 Desc);
1333}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001334
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001335llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1336 const char *Section,
1337 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001338 // Return null for empty list.
1339 if (Methods.empty())
1340 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1341
1342 std::vector<llvm::Constant*> Values(2);
1343 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1344 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1345 Methods.size());
1346 Values[1] = llvm::ConstantArray::get(AT, Methods);
1347 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1348
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001349 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001350 return llvm::ConstantExpr::getBitCast(GV,
1351 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001352}
1353
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001354/*
1355 struct _objc_category {
1356 char *category_name;
1357 char *class_name;
1358 struct _objc_method_list *instance_methods;
1359 struct _objc_method_list *class_methods;
1360 struct _objc_protocol_list *protocols;
1361 uint32_t size; // <rdar://4585769>
1362 struct _objc_property_list *instance_properties;
1363 };
1364 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001365void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001366 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001367
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001368 // FIXME: This is poor design, the OCD should have a pointer to the
1369 // category decl. Additionally, note that Category can be null for
1370 // the @implementation w/o an @interface case. Sema should just
1371 // create one for us as it does for @implementation so everyone else
1372 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001373 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001374 const ObjCCategoryDecl *Category =
1375 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001376 std::string ExtName(Interface->getNameAsString() + "_" +
1377 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001378
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001379 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1380 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1381 e = OCD->instmeth_end(); i != e; ++i) {
1382 // Instance methods should always be defined.
1383 InstanceMethods.push_back(GetMethodConstant(*i));
1384 }
1385 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1386 e = OCD->classmeth_end(); i != e; ++i) {
1387 // Class methods should always be defined.
1388 ClassMethods.push_back(GetMethodConstant(*i));
1389 }
1390
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001391 std::vector<llvm::Constant*> Values(7);
1392 Values[0] = GetClassName(OCD->getIdentifier());
1393 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001394 Values[2] =
1395 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1396 ExtName,
1397 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001398 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001399 Values[3] =
1400 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1401 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001402 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001403 if (Category) {
1404 Values[4] =
1405 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1406 Category->protocol_begin(),
1407 Category->protocol_end());
1408 } else {
1409 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1410 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001411 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001412
1413 // If there is no category @interface then there can be no properties.
1414 if (Category) {
1415 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001416 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001417 } else {
1418 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1419 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001420
1421 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1422 Values);
1423
1424 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001425 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1426 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001427 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001428 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001429}
1430
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431// FIXME: Get from somewhere?
1432enum ClassFlags {
1433 eClassFlags_Factory = 0x00001,
1434 eClassFlags_Meta = 0x00002,
1435 // <rdr://5142207>
1436 eClassFlags_HasCXXStructors = 0x02000,
1437 eClassFlags_Hidden = 0x20000,
1438 eClassFlags_ABI2_Hidden = 0x00010,
1439 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1440};
1441
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001442/*
1443 struct _objc_class {
1444 Class isa;
1445 Class super_class;
1446 const char *name;
1447 long version;
1448 long info;
1449 long instance_size;
1450 struct _objc_ivar_list *ivars;
1451 struct _objc_method_list *methods;
1452 struct _objc_cache *cache;
1453 struct _objc_protocol_list *protocols;
1454 // Objective-C 1.0 extensions (<rdr://4585769>)
1455 const char *ivar_layout;
1456 struct _objc_class_ext *ext;
1457 };
1458
1459 See EmitClassExtension();
1460 */
1461void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001462 DefinedSymbols.insert(ID->getIdentifier());
1463
Chris Lattner8ec03f52008-11-24 03:54:41 +00001464 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001465 // FIXME: Gross
1466 ObjCInterfaceDecl *Interface =
1467 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001468 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001469 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001470 Interface->protocol_begin(),
1471 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001472 const llvm::Type *InterfaceTy =
Chris Lattner03d9f342009-04-01 06:23:52 +00001473 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001475 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001476
1477 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001478 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001479 Flags |= eClassFlags_Hidden;
1480
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001481 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1482 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1483 e = ID->instmeth_end(); i != e; ++i) {
1484 // Instance methods should always be defined.
1485 InstanceMethods.push_back(GetMethodConstant(*i));
1486 }
1487 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1488 e = ID->classmeth_end(); i != e; ++i) {
1489 // Class methods should always be defined.
1490 ClassMethods.push_back(GetMethodConstant(*i));
1491 }
1492
1493 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1494 e = ID->propimpl_end(); i != e; ++i) {
1495 ObjCPropertyImplDecl *PID = *i;
1496
1497 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1498 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1499
1500 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1501 if (llvm::Constant *C = GetMethodConstant(MD))
1502 InstanceMethods.push_back(C);
1503 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1504 if (llvm::Constant *C = GetMethodConstant(MD))
1505 InstanceMethods.push_back(C);
1506 }
1507 }
1508
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001509 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001510 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001511 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001512 // Record a reference to the super class.
1513 LazySymbols.insert(Super->getIdentifier());
1514
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001515 Values[ 1] =
1516 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1517 ObjCTypes.ClassPtrTy);
1518 } else {
1519 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1520 }
1521 Values[ 2] = GetClassName(ID->getIdentifier());
1522 // Version is always 0.
1523 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1524 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1525 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001526 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001527 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001528 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001529 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001530 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001531 // cache is always NULL.
1532 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1533 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001534 // FIXME: Set ivar_layout
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001535 // Values[10] = BuildIvarLayout(ID, true);
1536 Values[10] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001537 Values[11] = EmitClassExtension(ID);
1538 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1539 Values);
1540
1541 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001542 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1543 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001544 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001545 DefinedClasses.push_back(GV);
1546}
1547
1548llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1549 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001550 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001551 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001552 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001553 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001554
Daniel Dunbar04d40782009-04-14 06:00:08 +00001555 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001556 Flags |= eClassFlags_Hidden;
1557
1558 std::vector<llvm::Constant*> Values(12);
1559 // The isa for the metaclass is the root of the hierarchy.
1560 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1561 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1562 Root = Super;
1563 Values[ 0] =
1564 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1565 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001566 // The super class for the metaclass is emitted as the name of the
1567 // super class. The runtime fixes this up to point to the
1568 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001569 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1570 Values[ 1] =
1571 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1572 ObjCTypes.ClassPtrTy);
1573 } else {
1574 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1575 }
1576 Values[ 2] = GetClassName(ID->getIdentifier());
1577 // Version is always 0.
1578 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1579 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1580 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001581 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001582 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001583 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001584 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001585 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001586 // cache is always NULL.
1587 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1588 Values[ 9] = Protocols;
1589 // ivar_layout for metaclass is always NULL.
1590 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1591 // The class extension is always unused for metaclasses.
1592 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1593 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1594 Values);
1595
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001596 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001597 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001598
1599 // Check for a forward reference.
1600 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1601 if (GV) {
1602 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1603 "Forward metaclass reference has incorrect type.");
1604 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1605 GV->setInitializer(Init);
1606 } else {
1607 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1608 llvm::GlobalValue::InternalLinkage,
1609 Init, Name,
1610 &CGM.getModule());
1611 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001612 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001613 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001614 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001615
1616 return GV;
1617}
1618
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001619llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001620 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001621
1622 // FIXME: Should we look these up somewhere other than the
1623 // module. Its a bit silly since we only generate these while
1624 // processing an implementation, so exactly one pointer would work
1625 // if know when we entered/exitted an implementation block.
1626
1627 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001628 // Previously, metaclass with internal linkage may have been defined.
1629 // pass 'true' as 2nd argument so it is returned.
1630 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001631 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1632 "Forward metaclass reference has incorrect type.");
1633 return GV;
1634 } else {
1635 // Generate as an external reference to keep a consistent
1636 // module. This will be patched up when we emit the metaclass.
1637 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1638 llvm::GlobalValue::ExternalLinkage,
1639 0,
1640 Name,
1641 &CGM.getModule());
1642 }
1643}
1644
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001645/*
1646 struct objc_class_ext {
1647 uint32_t size;
1648 const char *weak_ivar_layout;
1649 struct _objc_property_list *properties;
1650 };
1651*/
1652llvm::Constant *
1653CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1654 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001655 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001656
1657 std::vector<llvm::Constant*> Values(3);
1658 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001659 // FIXME: Output weak_ivar_layout string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001660 // Values[1] = BuildIvarLayout(ID, false);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001661 Values[1] = GetIvarLayoutName(0, ObjCTypes);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001662 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001663 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001664
1665 // Return null if no extension bits are used.
1666 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1667 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1668
1669 llvm::Constant *Init =
1670 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001671 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
1672 Init, 0, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001673}
1674
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001675/// countInheritedIvars - count number of ivars in class and its super class(s)
1676///
Douglas Gregor6ab35242009-04-09 21:40:53 +00001677static int countInheritedIvars(const ObjCInterfaceDecl *OI,
1678 ASTContext &Context) {
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001679 int count = 0;
1680 if (!OI)
1681 return 0;
1682 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1683 if (SuperClass)
Douglas Gregor6ab35242009-04-09 21:40:53 +00001684 count += countInheritedIvars(SuperClass, Context);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001685 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1686 E = OI->ivar_end(); I != E; ++I)
1687 ++count;
Fariborz Jahanian18191882009-03-31 18:11:23 +00001688 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001689 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
1690 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +00001691 if ((*I)->getPropertyIvarDecl())
1692 ++count;
1693 }
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001694 return count;
1695}
1696
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001697/// getInterfaceDeclForIvar - Get the interface declaration node where
1698/// this ivar is declared in.
1699/// FIXME. Ideally, this info should be in the ivar node. But currently
1700/// it is not and prevailing wisdom is that ASTs should not have more
1701/// info than is absolutely needed, even though this info reflects the
1702/// source language.
1703///
1704static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1705 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00001706 const ObjCIvarDecl *IVD,
1707 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001708 if (!OI)
1709 return 0;
1710 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1711 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1712 E = OI->ivar_end(); I != E; ++I)
1713 if ((*I)->getIdentifier() == IVD->getIdentifier())
1714 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00001715 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001716 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
1717 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00001718 ObjCPropertyDecl *PDecl = (*I);
1719 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
1720 if (IV->getIdentifier() == IVD->getIdentifier())
1721 return OI;
1722 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00001723 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001724}
1725
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001726/*
1727 struct objc_ivar {
1728 char *ivar_name;
1729 char *ivar_type;
1730 int ivar_offset;
1731 };
1732
1733 struct objc_ivar_list {
1734 int ivar_count;
1735 struct objc_ivar list[count];
1736 };
1737 */
1738llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001739 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001740 std::vector<llvm::Constant*> Ivars, Ivar(3);
1741
1742 // When emitting the root class GCC emits ivar entries for the
1743 // actual class structure. It is not clear if we need to follow this
1744 // behavior; for now lets try and get away with not doing it. If so,
1745 // the cleanest solution would be to make up an ObjCInterfaceDecl
1746 // for the class.
1747 if (ForClass)
1748 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001749
1750 ObjCInterfaceDecl *OID =
1751 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00001752 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001753
1754 RecordDecl::field_iterator ifield, pfield;
1755 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001756 for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext());
1757 ifield != e; ++ifield) {
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001758 FieldDecl *Field = *ifield;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001759 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001760 if (Field->getIdentifier())
1761 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1762 else
1763 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00001764 Ivar[1] = GetMethodVarType(Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001765 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001766 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001767 }
1768
1769 // Return null for empty list.
1770 if (Ivars.empty())
1771 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1772
1773 std::vector<llvm::Constant*> Values(2);
1774 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1775 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1776 Ivars.size());
1777 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1778 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1779
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001780 llvm::GlobalVariable *GV;
1781 if (ForClass)
1782 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00001783 Init, "__OBJC,__class_vars,regular,no_dead_strip",
1784 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001785 else
1786 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
1787 + ID->getNameAsString(),
1788 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
1789 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001790 return llvm::ConstantExpr::getBitCast(GV,
1791 ObjCTypes.IvarListPtrTy);
1792}
1793
1794/*
1795 struct objc_method {
1796 SEL method_name;
1797 char *method_types;
1798 void *method;
1799 };
1800
1801 struct objc_method_list {
1802 struct objc_method_list *obsolete;
1803 int count;
1804 struct objc_method methods_list[count];
1805 };
1806*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001807
1808/// GetMethodConstant - Return a struct objc_method constant for the
1809/// given method if it has been defined. The result is null if the
1810/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001811llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001812 // FIXME: Use DenseMap::lookup
1813 llvm::Function *Fn = MethodDefinitions[MD];
1814 if (!Fn)
1815 return 0;
1816
1817 std::vector<llvm::Constant*> Method(3);
1818 Method[0] =
1819 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1820 ObjCTypes.SelectorPtrTy);
1821 Method[1] = GetMethodVarType(MD);
1822 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1823 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1824}
1825
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001826llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1827 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001828 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001829 // Return null for empty list.
1830 if (Methods.empty())
1831 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1832
1833 std::vector<llvm::Constant*> Values(3);
1834 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1835 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1836 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1837 Methods.size());
1838 Values[2] = llvm::ConstantArray::get(AT, Methods);
1839 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1840
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001841 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001842 return llvm::ConstantExpr::getBitCast(GV,
1843 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001844}
1845
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001846llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001847 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001848 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001849 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001850
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001851 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001852 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001853 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001854 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001855 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001856 llvm::GlobalValue::InternalLinkage,
1857 Name,
1858 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001859 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001860
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001861 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001862}
1863
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001864uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00001865 const FieldDecl *Field) {
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00001866 if (!Field->isBitField())
1867 return Layout->getElementOffset(
1868 CGM.getTypes().getLLVMFieldNo(Field));
1869 // FIXME. Must be a better way of getting a bitfield base offset.
1870 uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field);
1871 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
1872 uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
1873 offset = (offset*size)/8;
1874 return offset;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001875}
1876
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001877/// GetFieldBaseOffset - return's field byt offset.
1878uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
1879 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00001880 const FieldDecl *Field) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001881 const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Chris Lattnercd0ee142009-03-31 08:33:16 +00001882 const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1883 return GetIvarBaseOffset(Layout, FD);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001884}
1885
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001886llvm::GlobalVariable *
1887CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
1888 llvm::Constant *Init,
1889 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001890 unsigned Align,
1891 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001892 const llvm::Type *Ty = Init->getType();
1893 llvm::GlobalVariable *GV =
1894 new llvm::GlobalVariable(Ty, false,
1895 llvm::GlobalValue::InternalLinkage,
1896 Init,
1897 Name,
1898 &CGM.getModule());
1899 if (Section)
1900 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001901 if (Align)
1902 GV->setAlignment(Align);
1903 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001904 UsedGlobals.push_back(GV);
1905 return GV;
1906}
1907
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001908llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001909 // Abuse this interface function as a place to finalize.
1910 FinishModule();
1911
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001912 return NULL;
1913}
1914
Chris Lattner74391b42009-03-22 21:03:39 +00001915llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001916 return ObjCTypes.GetPropertyFn;
1917}
1918
Chris Lattner74391b42009-03-22 21:03:39 +00001919llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001920 return ObjCTypes.SetPropertyFn;
1921}
1922
Chris Lattner74391b42009-03-22 21:03:39 +00001923llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001924 return ObjCTypes.EnumerationMutationFn;
1925}
1926
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001927/*
1928
1929Objective-C setjmp-longjmp (sjlj) Exception Handling
1930--
1931
1932The basic framework for a @try-catch-finally is as follows:
1933{
1934 objc_exception_data d;
1935 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001936 bool _call_try_exit = true;
1937
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001938 objc_exception_try_enter(&d);
1939 if (!setjmp(d.jmp_buf)) {
1940 ... try body ...
1941 } else {
1942 // exception path
1943 id _caught = objc_exception_extract(&d);
1944
1945 // enter new try scope for handlers
1946 if (!setjmp(d.jmp_buf)) {
1947 ... match exception and execute catch blocks ...
1948
1949 // fell off end, rethrow.
1950 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001951 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001952 } else {
1953 // exception in catch block
1954 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001955 _call_try_exit = false;
1956 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001957 }
1958 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001959 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001960
1961finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001962 if (_call_try_exit)
1963 objc_exception_try_exit(&d);
1964
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001965 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001966 ... dispatch to finally destination ...
1967
1968finally_rethrow:
1969 objc_exception_throw(_rethrow);
1970
1971finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001972}
1973
1974This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001975uses _rethrow to determine if objc_exception_try_exit should be called
1976and if the object should be rethrown. This breaks in the face of
1977throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001978
1979We specialize this framework for a few particular circumstances:
1980
1981 - If there are no catch blocks, then we avoid emitting the second
1982 exception handling context.
1983
1984 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1985 e)) we avoid emitting the code to rethrow an uncaught exception.
1986
1987 - FIXME: If there is no @finally block we can do a few more
1988 simplifications.
1989
1990Rethrows and Jumps-Through-Finally
1991--
1992
1993Support for implicit rethrows and jumping through the finally block is
1994handled by storing the current exception-handling context in
1995ObjCEHStack.
1996
Daniel Dunbar898d5082008-09-30 01:06:03 +00001997In order to implement proper @finally semantics, we support one basic
1998mechanism for jumping through the finally block to an arbitrary
1999destination. Constructs which generate exits from a @try or @catch
2000block use this mechanism to implement the proper semantics by chaining
2001jumps, as necessary.
2002
2003This mechanism works like the one used for indirect goto: we
2004arbitrarily assign an ID to each destination and store the ID for the
2005destination in a variable prior to entering the finally block. At the
2006end of the finally block we simply create a switch to the proper
2007destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002008
2009Code gen for @synchronized(expr) stmt;
2010Effectively generating code for:
2011objc_sync_enter(expr);
2012@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002013*/
2014
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002015void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2016 const Stmt &S) {
2017 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002018 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002019 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002020 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002021 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2022 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2023 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002024
2025 // For @synchronized, call objc_sync_enter(sync.expr). The
2026 // evaluation of the expression must occur before we enter the
2027 // @synchronized. We can safely avoid a temp here because jumps into
2028 // @synchronized are illegal & this will dominate uses.
2029 llvm::Value *SyncArg = 0;
2030 if (!isTry) {
2031 SyncArg =
2032 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2033 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002034 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002035 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002036
2037 // Push an EH context entry, used for handling rethrows and jumps
2038 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002039 CGF.PushCleanupBlock(FinallyBlock);
2040
Anders Carlsson273558f2009-02-07 21:37:21 +00002041 CGF.ObjCEHValueStack.push_back(0);
2042
Daniel Dunbar898d5082008-09-30 01:06:03 +00002043 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002044 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2045 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002046 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2047 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002048 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2049 "_call_try_exit");
2050 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2051
Anders Carlsson80f25672008-09-09 17:59:25 +00002052 // Enter a new try block and call setjmp.
2053 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
2054 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2055 "jmpbufarray");
2056 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
2057 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2058 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002059
Daniel Dunbar55e87422008-11-11 02:29:29 +00002060 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2061 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002062 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002063 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002064
2065 // Emit the @try block.
2066 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002067 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2068 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002069 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002070
2071 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002072 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002073
2074 // Retrieve the exception object. We may emit multiple blocks but
2075 // nothing can cross this so the value is already in SSA form.
2076 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2077 ExceptionData,
2078 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002079 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002080 if (!isTry)
2081 {
2082 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002083 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002084 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002085 }
2086 else if (const ObjCAtCatchStmt* CatchStmt =
2087 cast<ObjCAtTryStmt>(S).getCatchStmts())
2088 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002089 // Enter a new exception try block (in case a @catch block throws
2090 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00002091 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002092
Anders Carlsson80f25672008-09-09 17:59:25 +00002093 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2094 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002095 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002096
Daniel Dunbar55e87422008-11-11 02:29:29 +00002097 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2098 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002099 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002100
2101 CGF.EmitBlock(CatchBlock);
2102
Daniel Dunbar55e40722008-09-27 07:03:52 +00002103 // Handle catch list. As a special case we check if everything is
2104 // matched and avoid generating code for falling off the end if
2105 // so.
2106 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002107 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002108 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002109
Steve Naroff7ba138a2009-03-03 19:52:17 +00002110 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002111 const PointerType *PT = 0;
2112
Anders Carlsson80f25672008-09-09 17:59:25 +00002113 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002114 if (!CatchParam) {
2115 AllMatched = true;
2116 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002117 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002118
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002119 // catch(id e) always matches.
2120 // FIXME: For the time being we also match id<X>; this should
2121 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002122 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002123 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002124 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002125 }
2126
Daniel Dunbar55e40722008-09-27 07:03:52 +00002127 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002128 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002129 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002130 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002131 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002132 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002133
Anders Carlssondde0a942008-09-11 09:15:33 +00002134 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002135 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002136 break;
2137 }
2138
Daniel Dunbar129271a2008-09-27 07:36:24 +00002139 assert(PT && "Unexpected non-pointer type in @catch");
2140 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002141 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002142 assert(ObjCType && "Catch parameter must have Objective-C type!");
2143
2144 // Check if the @catch block matches the exception object.
2145 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2146
Anders Carlsson80f25672008-09-09 17:59:25 +00002147 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2148 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002149
Daniel Dunbar55e87422008-11-11 02:29:29 +00002150 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002151
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002152 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002153 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002154
2155 // Emit the @catch block.
2156 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002157 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002158 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002159
2160 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002161 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002162 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002163 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002164
2165 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002166 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002167
2168 CGF.EmitBlock(NextCatchBlock);
2169 }
2170
Daniel Dunbar55e40722008-09-27 07:03:52 +00002171 if (!AllMatched) {
2172 // None of the handlers caught the exception, so store it to be
2173 // rethrown at the end of the @finally block.
2174 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002175 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002176 }
2177
2178 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002179 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002180 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2181 ExceptionData),
2182 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002183 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002184 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002185 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002186 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002187 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002188 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002189 }
2190
Daniel Dunbar898d5082008-09-30 01:06:03 +00002191 // Pop the exception-handling stack entry. It is important to do
2192 // this now, because the code in the @finally block is not in this
2193 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002194 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2195
Anders Carlsson273558f2009-02-07 21:37:21 +00002196 CGF.ObjCEHValueStack.pop_back();
2197
Anders Carlsson80f25672008-09-09 17:59:25 +00002198 // Emit the @finally block.
2199 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002200 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2201
2202 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2203
2204 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002205 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002206
2207 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002208 if (isTry) {
2209 if (const ObjCAtFinallyStmt* FinallyStmt =
2210 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2211 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002212 } else {
2213 // Emit objc_sync_exit(expr); as finally's sole statement for
2214 // @synchronized.
2215 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002216 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002217
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002218 // Emit the switch block
2219 if (Info.SwitchBlock)
2220 CGF.EmitBlock(Info.SwitchBlock);
2221 if (Info.EndBlock)
2222 CGF.EmitBlock(Info.EndBlock);
2223
Daniel Dunbar898d5082008-09-30 01:06:03 +00002224 CGF.EmitBlock(FinallyRethrow);
2225 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2226 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002227 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002228
2229 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002230}
2231
2232void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002233 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002234 llvm::Value *ExceptionAsObject;
2235
2236 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2237 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2238 ExceptionAsObject =
2239 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2240 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002241 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002242 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002243 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002244 }
2245
2246 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002247 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002248
2249 // Clear the insertion point to indicate we are in unreachable code.
2250 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002251}
2252
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002253/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002254/// object: objc_read_weak (id *src)
2255///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002256llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002257 llvm::Value *AddrWeakObj)
2258{
Eli Friedman8339b352009-03-07 03:57:15 +00002259 const llvm::Type* DestTy =
2260 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002261 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002262 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002263 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002264 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002265 return read_weak;
2266}
2267
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002268/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2269/// objc_assign_weak (id src, id *dst)
2270///
2271void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2272 llvm::Value *src, llvm::Value *dst)
2273{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002274 const llvm::Type * SrcTy = src->getType();
2275 if (!isa<llvm::PointerType>(SrcTy)) {
2276 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2277 assert(Size <= 8 && "does not support size > 8");
2278 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2279 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002280 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2281 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002282 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2283 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002284 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2285 src, dst, "weakassign");
2286 return;
2287}
2288
Fariborz Jahanian58626502008-11-19 00:59:10 +00002289/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2290/// objc_assign_global (id src, id *dst)
2291///
2292void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2293 llvm::Value *src, llvm::Value *dst)
2294{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002295 const llvm::Type * SrcTy = src->getType();
2296 if (!isa<llvm::PointerType>(SrcTy)) {
2297 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2298 assert(Size <= 8 && "does not support size > 8");
2299 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2300 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002301 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2302 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002303 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2304 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002305 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2306 src, dst, "globalassign");
2307 return;
2308}
2309
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002310/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2311/// objc_assign_ivar (id src, id *dst)
2312///
2313void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2314 llvm::Value *src, llvm::Value *dst)
2315{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002316 const llvm::Type * SrcTy = src->getType();
2317 if (!isa<llvm::PointerType>(SrcTy)) {
2318 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2319 assert(Size <= 8 && "does not support size > 8");
2320 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2321 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002322 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2323 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002324 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2325 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2326 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2327 src, dst, "assignivar");
2328 return;
2329}
2330
Fariborz Jahanian58626502008-11-19 00:59:10 +00002331/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2332/// objc_assign_strongCast (id src, id *dst)
2333///
2334void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2335 llvm::Value *src, llvm::Value *dst)
2336{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002337 const llvm::Type * SrcTy = src->getType();
2338 if (!isa<llvm::PointerType>(SrcTy)) {
2339 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2340 assert(Size <= 8 && "does not support size > 8");
2341 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2342 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002343 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2344 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002345 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2346 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002347 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2348 src, dst, "weakassign");
2349 return;
2350}
2351
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002352/// EmitObjCValueForIvar - Code Gen for ivar reference.
2353///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002354LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2355 QualType ObjectTy,
2356 llvm::Value *BaseValue,
2357 const ObjCIvarDecl *Ivar,
2358 const FieldDecl *Field,
2359 unsigned CVRQualifiers) {
2360 if (Ivar->isBitField())
2361 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2362 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002363 // TODO: Add a special case for isa (index 0)
2364 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2365 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002366 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002367 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2368 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002369 LValue::SetObjCIvar(LV, true);
2370 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002371}
2372
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002373llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2374 ObjCInterfaceDecl *Interface,
2375 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002376 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002377 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00002378 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002379 return llvm::ConstantInt::get(
2380 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2381 Offset);
2382}
2383
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002384/* *** Private Interface *** */
2385
2386/// EmitImageInfo - Emit the image info marker used to encode some module
2387/// level information.
2388///
2389/// See: <rdr://4810609&4810587&4810587>
2390/// struct IMAGE_INFO {
2391/// unsigned version;
2392/// unsigned flags;
2393/// };
2394enum ImageInfoFlags {
2395 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2396 eImageInfo_GarbageCollected = (1 << 1),
2397 eImageInfo_GCOnly = (1 << 2)
2398};
2399
2400void CGObjCMac::EmitImageInfo() {
2401 unsigned version = 0; // Version is unused?
2402 unsigned flags = 0;
2403
2404 // FIXME: Fix and continue?
2405 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2406 flags |= eImageInfo_GarbageCollected;
2407 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2408 flags |= eImageInfo_GCOnly;
2409
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002410 // Emitted as int[2];
2411 llvm::Constant *values[2] = {
2412 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2413 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2414 };
2415 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002416
2417 const char *Section;
2418 if (ObjCABI == 1)
2419 Section = "__OBJC, __image_info,regular";
2420 else
2421 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002422 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002423 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2424 llvm::ConstantArray::get(AT, values, 2),
2425 Section,
2426 0,
2427 true);
2428 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002429}
2430
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002431
2432// struct objc_module {
2433// unsigned long version;
2434// unsigned long size;
2435// const char *name;
2436// Symtab symtab;
2437// };
2438
2439// FIXME: Get from somewhere
2440static const int ModuleVersion = 7;
2441
2442void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002443 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002444
2445 std::vector<llvm::Constant*> Values(4);
2446 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2447 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002448 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002449 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002450 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002451 CreateMetadataVar("\01L_OBJC_MODULES",
2452 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2453 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002454 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002455}
2456
2457llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 unsigned NumClasses = DefinedClasses.size();
2459 unsigned NumCategories = DefinedCategories.size();
2460
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002461 // Return null if no symbols were defined.
2462 if (!NumClasses && !NumCategories)
2463 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2464
2465 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002466 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2467 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2468 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2469 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2470
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002471 // The runtime expects exactly the list of defined classes followed
2472 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002474 for (unsigned i=0; i<NumClasses; i++)
2475 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2476 ObjCTypes.Int8PtrTy);
2477 for (unsigned i=0; i<NumCategories; i++)
2478 Symbols[NumClasses + i] =
2479 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2480 ObjCTypes.Int8PtrTy);
2481
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002482 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002483 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484 NumClasses + NumCategories),
2485 Symbols);
2486
2487 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2488
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002489 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002490 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2491 "__OBJC,__symbols,regular,no_dead_strip",
2492 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002493 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2494}
2495
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002496llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002497 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002498 LazySymbols.insert(ID->getIdentifier());
2499
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2501
2502 if (!Entry) {
2503 llvm::Constant *Casted =
2504 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2505 ObjCTypes.ClassPtrTy);
2506 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002507 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2508 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
2509 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002510 }
2511
2512 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002513}
2514
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002515llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002516 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2517
2518 if (!Entry) {
2519 llvm::Constant *Casted =
2520 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2521 ObjCTypes.SelectorPtrTy);
2522 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002523 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2524 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
2525 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002526 }
2527
2528 return Builder.CreateLoad(Entry, false, "tmp");
2529}
2530
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002531llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002532 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002533
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002534 if (!Entry)
2535 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2536 llvm::ConstantArray::get(Ident->getName()),
2537 "__TEXT,__cstring,cstring_literals",
2538 0, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002539
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002540 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002541}
2542
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002543/// GetInterfaceDeclStructLayout - Get layout for ivars of given
2544/// interface declaration.
2545const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout(
2546 const ObjCInterfaceDecl *OID) const {
2547 const llvm::Type *InterfaceTy =
2548 CGM.getTypes().ConvertType(
2549 CGM.getContext().getObjCInterfaceType(
2550 const_cast<ObjCInterfaceDecl*>(OID)));
2551 const llvm::StructLayout *Layout =
2552 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
2553 return Layout;
2554}
2555
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002556/// GetIvarLayoutName - Returns a unique constant for the given
2557/// ivar layout bitmap.
2558llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2559 const ObjCCommonTypesHelper &ObjCTypes) {
2560 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2561}
2562
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002563void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2564 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002565 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002566 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002567 unsigned int BytePos, bool ForStrongLayout,
2568 int &Index, int &SkIndex, bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002569 bool IsUnion = (RD && RD->isUnion());
2570 uint64_t MaxUnionIvarSize = 0;
2571 uint64_t MaxSkippedUnionIvarSize = 0;
2572 FieldDecl *MaxField = 0;
2573 FieldDecl *MaxSkippedField = 0;
Chris Lattnerf1690852009-03-31 08:48:01 +00002574 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002575 if (RecFields.empty())
2576 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002577 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002578 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002579 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2580 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2581
2582 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2583
2584 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002585 FieldDecl *Field = RecFields[i];
2586 // Skip over unnamed or bitfields
2587 if (!Field->getIdentifier() || Field->isBitField())
2588 continue;
2589 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002590 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002591 if (FQT->isUnionType())
2592 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002593 else
2594 assert(FQT->isRecordType() &&
2595 "only union/record is supported for ivar layout bitmap");
2596
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002597 const RecordType *RT = FQT->getAsRecordType();
2598 const RecordDecl *RD = RT->getDecl();
2599 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002600 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2601 RD->field_end(CGM.getContext()));
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002602 // FIXME - Is Layout correct?
Chris Lattnerf1690852009-03-31 08:48:01 +00002603 BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002604 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002605 ForStrongLayout, Index, SkIndex,
2606 HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002607 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002608 continue;
2609 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002610
2611 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002612 const ConstantArrayType *CArray =
2613 dyn_cast_or_null<ConstantArrayType>(Array);
2614 assert(CArray && "only array with know element size is supported");
2615 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002616 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2617 const ConstantArrayType *CArray =
2618 dyn_cast_or_null<ConstantArrayType>(Array);
2619 FQT = CArray->getElementType();
2620 }
2621
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002622 assert(!FQT->isUnionType() &&
2623 "layout for array of unions not supported");
2624 if (FQT->isRecordType()) {
2625 uint64_t ElCount = CArray->getSize().getZExtValue();
2626 int OldIndex = Index;
2627 int OldSkIndex = SkIndex;
2628
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002629 // FIXME - Use a common routine with the above!
2630 const RecordType *RT = FQT->getAsRecordType();
2631 const RecordDecl *RD = RT->getDecl();
2632 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002633 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2634 RD->field_end(CGM.getContext()));
Chris Lattnerf1690852009-03-31 08:48:01 +00002635
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002636 BuildAggrIvarLayout(OI, Layout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002637 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002638 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002639 ForStrongLayout, Index, SkIndex,
2640 HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002641 TmpRecFields.clear();
2642
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002643 // Replicate layout information for each array element. Note that
2644 // one element is already done.
2645 uint64_t ElIx = 1;
2646 for (int FirstIndex = Index, FirstSkIndex = SkIndex;
2647 ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002648 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002649 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2650 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002651 GC_IVAR gcivar;
2652 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
2653 gcivar.ivar_size = IvarsInfo[i].ivar_size;
2654 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002655 }
2656
Chris Lattnerf1690852009-03-31 08:48:01 +00002657 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002658 GC_IVAR skivar;
2659 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
2660 skivar.ivar_size = SkipIvars[i].ivar_size;
2661 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002662 }
2663 }
2664 continue;
2665 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002666 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002667 // At this point, we are done with Record/Union and array there of.
2668 // For other arrays we are down to its element type.
2669 QualType::GCAttrTypes GCAttr = QualType::GCNone;
2670 do {
2671 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
2672 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
2673 break;
2674 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002675 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002676 GCAttr = QualType::Strong;
2677 break;
2678 }
2679 else if (const PointerType *PT = FQT->getAsPointerType()) {
2680 FQT = PT->getPointeeType();
2681 }
2682 else {
2683 break;
2684 }
2685 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +00002686
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002687 if ((ForStrongLayout && GCAttr == QualType::Strong)
2688 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
2689 if (IsUnion)
2690 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002691 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
2692 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002693 if (UnionIvarSize > MaxUnionIvarSize)
2694 {
2695 MaxUnionIvarSize = UnionIvarSize;
2696 MaxField = Field;
2697 }
2698 }
2699 else
2700 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002701 GC_IVAR gcivar;
2702 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2703 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2704 WordSizeInBits;
2705 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002706 }
2707 }
2708 else if ((ForStrongLayout &&
2709 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
2710 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
2711 if (IsUnion)
2712 {
2713 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
2714 if (UnionIvarSize > MaxSkippedUnionIvarSize)
2715 {
2716 MaxSkippedUnionIvarSize = UnionIvarSize;
2717 MaxSkippedField = Field;
2718 }
2719 }
2720 else
2721 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002722 GC_IVAR skivar;
2723 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2724 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2725 WordSizeInBits;
2726 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002727 }
2728 }
2729 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002730 if (MaxField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002731 GC_IVAR gcivar;
2732 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
2733 gcivar.ivar_size = MaxUnionIvarSize;
2734 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002735 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002736
2737 if (MaxSkippedField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002738 GC_IVAR skivar;
2739 skivar.ivar_bytepos = BytePos +
2740 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
2741 skivar.ivar_size = MaxSkippedUnionIvarSize;
2742 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002743 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002744}
2745
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002746static int
Chris Lattnerf1690852009-03-31 08:48:01 +00002747IvarBytePosCompare(const void *a, const void *b)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002748{
2749 unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos;
2750 unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos;
2751
2752 if (sa < sb)
2753 return -1;
2754 if (sa > sb)
2755 return 1;
2756 return 0;
2757}
2758
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002759/// BuildIvarLayout - Builds ivar layout bitmap for the class
2760/// implementation for the __strong or __weak case.
2761/// The layout map displays which words in ivar list must be skipped
2762/// and which must be scanned by GC (see below). String is built of bytes.
2763/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
2764/// of words to skip and right nibble is count of words to scan. So, each
2765/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
2766/// represented by a 0x00 byte which also ends the string.
2767/// 1. when ForStrongLayout is true, following ivars are scanned:
2768/// - id, Class
2769/// - object *
2770/// - __strong anything
2771///
2772/// 2. When ForStrongLayout is false, following ivars are scanned:
2773/// - __weak anything
2774///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002775llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002776 const ObjCImplementationDecl *OMD,
2777 bool ForStrongLayout) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002778 int Index = -1;
2779 int SkIndex = -1;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002780 bool hasUnion = false;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002781 int SkipScan;
2782 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002783 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2784 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
2785 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002786
Chris Lattnerf1690852009-03-31 08:48:01 +00002787 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002788 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002789 CGM.getContext().CollectObjCIvars(OI, RecFields);
2790 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002791 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00002792
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002793 SkipIvars.clear();
2794 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002795
2796 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI);
Chris Lattnerf1690852009-03-31 08:48:01 +00002797 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout,
2798 Index, SkIndex, hasUnion);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002799 if (Index == -1)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002800 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002801
2802 // Sort on byte position in case we encounterred a union nested in
2803 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002804 if (hasUnion && !IvarsInfo.empty())
2805 qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2806 if (hasUnion && !SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002807 qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2808
2809 // Build the string of skip/scan nibbles
2810 SkipScan = -1;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002811 SkipScanIvars.clear();
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002812 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002813 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002814 if (IvarsInfo[0].ivar_bytepos == 0) {
2815 WordsToSkip = 0;
2816 WordsToScan = IvarsInfo[0].ivar_size;
2817 }
2818 else {
2819 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
2820 WordsToScan = IvarsInfo[0].ivar_size;
2821 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002822 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002823 {
2824 unsigned int TailPrevGCObjC =
2825 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
2826 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
2827 {
2828 // consecutive 'scanned' object pointers.
2829 WordsToScan += IvarsInfo[i].ivar_size;
2830 }
2831 else
2832 {
2833 // Skip over 'gc'able object pointer which lay over each other.
2834 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
2835 continue;
2836 // Must skip over 1 or more words. We save current skip/scan values
2837 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002838 SKIP_SCAN SkScan;
2839 SkScan.skip = WordsToSkip;
2840 SkScan.scan = WordsToScan;
2841 SkipScanIvars.push_back(SkScan); ++SkipScan;
2842
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002843 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002844 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
2845 SkScan.scan = 0;
2846 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002847 WordsToSkip = 0;
2848 WordsToScan = IvarsInfo[i].ivar_size;
2849 }
2850 }
2851 if (WordsToScan > 0)
2852 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002853 SKIP_SCAN SkScan;
2854 SkScan.skip = WordsToSkip;
2855 SkScan.scan = WordsToScan;
2856 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002857 }
2858
2859 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002860 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002861 {
2862 int LastByteSkipped =
2863 SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size;
2864 int LastByteScanned =
2865 IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize;
2866 BytesSkipped = (LastByteSkipped > LastByteScanned);
2867 // Compute number of bytes to skip at the tail end of the last ivar scanned.
2868 if (BytesSkipped)
2869 {
2870 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002871 SKIP_SCAN SkScan;
2872 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
2873 SkScan.scan = 0;
2874 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002875 }
2876 }
2877 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
2878 // as 0xMN.
2879 for (int i = 0; i <= SkipScan; i++)
2880 {
2881 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
2882 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
2883 // 0xM0 followed by 0x0N detected.
2884 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
2885 for (int j = i+1; j < SkipScan; j++)
2886 SkipScanIvars[j] = SkipScanIvars[j+1];
2887 --SkipScan;
2888 }
2889 }
2890
2891 // Generate the string.
2892 std::string BitMap;
2893 for (int i = 0; i <= SkipScan; i++)
2894 {
2895 unsigned char byte;
2896 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
2897 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
2898 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
2899 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
2900
2901 if (skip_small > 0 || skip_big > 0)
2902 BytesSkipped = true;
2903 // first skip big.
2904 for (unsigned int ix = 0; ix < skip_big; ix++)
2905 BitMap += (unsigned char)(0xf0);
2906
2907 // next (skip small, scan)
2908 if (skip_small)
2909 {
2910 byte = skip_small << 4;
2911 if (scan_big > 0)
2912 {
2913 byte |= 0xf;
2914 --scan_big;
2915 }
2916 else if (scan_small)
2917 {
2918 byte |= scan_small;
2919 scan_small = 0;
2920 }
2921 BitMap += byte;
2922 }
2923 // next scan big
2924 for (unsigned int ix = 0; ix < scan_big; ix++)
2925 BitMap += (unsigned char)(0x0f);
2926 // last scan small
2927 if (scan_small)
2928 {
2929 byte = scan_small;
2930 BitMap += byte;
2931 }
2932 }
2933 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002934 unsigned char zero = 0;
2935 BitMap += zero;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002936 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
2937 // final layout.
2938 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002939 return llvm::Constant::getNullValue(PtrTy);
2940 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2941 llvm::ConstantArray::get(BitMap.c_str()),
2942 "__TEXT,__cstring,cstring_literals",
2943 0, true);
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002944 // FIXME. Need a commomand-line option for this eventually.
2945 if (ForStrongLayout)
2946 printf("\nstrong ivar layout: ");
2947 else
2948 printf("\nweak ivar layout: ");
2949 const unsigned char *s = (unsigned char*)BitMap.c_str();
2950 for (unsigned i = 0; i < BitMap.size(); i++)
Fariborz Jahaniandbf15cb2009-03-26 19:10:36 +00002951 if (!(s[i] & 0xf0))
2952 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
2953 else
2954 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002955 printf("\n");
2956
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002957 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002958}
2959
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002960llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002961 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2962
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002963 // FIXME: Avoid std::string copying.
2964 if (!Entry)
2965 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
2966 llvm::ConstantArray::get(Sel.getAsString()),
2967 "__TEXT,__cstring,cstring_literals",
2968 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002969
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002970 return getConstantGEP(Entry, 0, 0);
2971}
2972
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002973// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002974llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002975 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2976}
2977
2978// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002979llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002980 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2981}
2982
Devang Patel7794bb82009-03-04 18:21:39 +00002983llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) {
2984 std::string TypeStr;
2985 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
2986
2987 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002988
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002989 if (!Entry)
2990 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
2991 llvm::ConstantArray::get(TypeStr),
2992 "__TEXT,__cstring,cstring_literals",
2993 0, true);
2994
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002995 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002996}
2997
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002998llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002999 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003000 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3001 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003002
3003 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3004
3005 if (!Entry) {
3006 llvm::Constant *C = llvm::ConstantArray::get(TypeStr);
3007 Entry =
3008 new llvm::GlobalVariable(C->getType(), false,
3009 llvm::GlobalValue::InternalLinkage,
3010 C, "\01L_OBJC_METH_VAR_TYPE_",
3011 &CGM.getModule());
3012 Entry->setSection("__TEXT,__cstring,cstring_literals");
3013 UsedGlobals.push_back(Entry);
3014 }
3015
3016 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003017}
3018
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003019// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003020llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003021 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3022
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003023 if (!Entry)
3024 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3025 llvm::ConstantArray::get(Ident->getName()),
3026 "__TEXT,__cstring,cstring_literals",
3027 0, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003028
3029 return getConstantGEP(Entry, 0, 0);
3030}
3031
3032// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003033// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003034llvm::Constant *
3035 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3036 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003037 std::string TypeStr;
3038 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003039 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3040}
3041
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003042void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3043 const ObjCContainerDecl *CD,
3044 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003045 NameOut = '\01';
3046 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003047 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003048 assert (CD && "Missing container decl in GetNameForMethod");
3049 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00003050 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
3051 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00003052 NameOut += ' ';
3053 NameOut += D->getSelector().getAsString();
3054 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003055}
3056
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003057/// GetFirstIvarInRecord - This routine returns the record for the
3058/// implementation of the fiven class OID. It also returns field
3059/// corresponding to the first ivar in the class in FIV. It also
3060/// returns the one before the first ivar.
3061///
3062const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
3063 const ObjCInterfaceDecl *OID,
3064 RecordDecl::field_iterator &FIV,
3065 RecordDecl::field_iterator &PIV) {
Douglas Gregor6ab35242009-04-09 21:40:53 +00003066 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(),
3067 CGM.getContext());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003068 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
Douglas Gregor6ab35242009-04-09 21:40:53 +00003069 RecordDecl::field_iterator ifield = RD->field_begin(CGM.getContext());
3070 RecordDecl::field_iterator pfield = RD->field_end(CGM.getContext());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003071 while (countSuperClassIvars-- > 0) {
3072 pfield = ifield;
3073 ++ifield;
3074 }
3075 FIV = ifield;
3076 PIV = pfield;
3077 return RD;
3078}
3079
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003080void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003081 EmitModuleInfo();
3082
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003083 // Emit the dummy bodies for any protocols which were referenced but
3084 // never defined.
3085 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3086 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3087 if (i->second->hasInitializer())
3088 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003089
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003090 std::vector<llvm::Constant*> Values(5);
3091 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3092 Values[1] = GetClassName(i->first);
3093 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3094 Values[3] = Values[4] =
3095 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3096 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3097 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3098 Values));
3099 }
3100
3101 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003102 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003103 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003104 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003105 }
3106
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003107 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003108 llvm::GlobalValue *GV =
3109 new llvm::GlobalVariable(AT, false,
3110 llvm::GlobalValue::AppendingLinkage,
3111 llvm::ConstantArray::get(AT, Used),
3112 "llvm.used",
3113 &CGM.getModule());
3114
3115 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003116
3117 // Add assembler directives to add lazy undefined symbol references
3118 // for classes which are referenced but not defined. This is
3119 // important for correct linker interaction.
3120
3121 // FIXME: Uh, this isn't particularly portable.
3122 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003123
3124 if (!CGM.getModule().getModuleInlineAsm().empty())
3125 s << "\n";
3126
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003127 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3128 e = LazySymbols.end(); i != e; ++i) {
3129 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3130 }
3131 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3132 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003133 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003134 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3135 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003136
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003137 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003138}
3139
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003140CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003141 : CGObjCCommonMac(cgm),
3142 ObjCTypes(cgm)
3143{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003144 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003145 ObjCABI = 2;
3146}
3147
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003148/* *** */
3149
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003150ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3151: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003152{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003153 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3154 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003155
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003156 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003157 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003158 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003159 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003160 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3161
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003162 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003163 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003164 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003165
3166 // FIXME: It would be nice to unify this with the opaque type, so
3167 // that the IR comes out a bit cleaner.
3168 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3169 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003170
3171 // I'm not sure I like this. The implicit coordination is a bit
3172 // gross. We should solve this in a reasonable fashion because this
3173 // is a pretty common task (match some runtime data structure with
3174 // an LLVM data structure).
3175
3176 // FIXME: This is leaked.
3177 // FIXME: Merge with rewriter code?
3178
3179 // struct _objc_super {
3180 // id self;
3181 // Class cls;
3182 // }
3183 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3184 SourceLocation(),
3185 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003186 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3187 Ctx.getObjCIdType(), 0, false));
3188 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3189 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003190 RD->completeDefinition(Ctx);
3191
3192 SuperCTy = Ctx.getTagDeclType(RD);
3193 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3194
3195 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003196 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3197
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003198 // struct _prop_t {
3199 // char *name;
3200 // char *attributes;
3201 // }
3202 PropertyTy = llvm::StructType::get(Int8PtrTy,
3203 Int8PtrTy,
3204 NULL);
3205 CGM.getModule().addTypeName("struct._prop_t",
3206 PropertyTy);
3207
3208 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003209 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003210 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003211 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003212 // }
3213 PropertyListTy = llvm::StructType::get(IntTy,
3214 IntTy,
3215 llvm::ArrayType::get(PropertyTy, 0),
3216 NULL);
3217 CGM.getModule().addTypeName("struct._prop_list_t",
3218 PropertyListTy);
3219 // struct _prop_list_t *
3220 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3221
3222 // struct _objc_method {
3223 // SEL _cmd;
3224 // char *method_type;
3225 // char *_imp;
3226 // }
3227 MethodTy = llvm::StructType::get(SelectorPtrTy,
3228 Int8PtrTy,
3229 Int8PtrTy,
3230 NULL);
3231 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003232
3233 // struct _objc_cache *
3234 CacheTy = llvm::OpaqueType::get();
3235 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3236 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003237
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003238 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003239
3240 QualType IdType = Ctx.getObjCIdType();
3241 QualType SelType = Ctx.getObjCSelType();
3242 llvm::SmallVector<QualType,16> Params;
3243 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003244
3245 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003246 Params.push_back(IdType);
3247 Params.push_back(SelType);
3248 Params.push_back(Ctx.LongTy);
3249 Params.push_back(Ctx.BoolTy);
3250 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
3251 false);
3252 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003253
3254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
3255 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003256 Params.push_back(IdType);
3257 Params.push_back(SelType);
3258 Params.push_back(Ctx.LongTy);
3259 Params.push_back(IdType);
3260 Params.push_back(Ctx.BoolTy);
3261 Params.push_back(Ctx.BoolTy);
3262 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3263 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
3264
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003265 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003266
3267 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003268 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003269 Params.push_back(IdType);
3270 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3271 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
3272 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003273
3274 // gc's API
3275 // id objc_read_weak (id *)
3276 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003277 Params.push_back(Ctx.getPointerType(IdType));
3278 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3279 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
3280
3281 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003282 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003283 Params.push_back(IdType);
3284 Params.push_back(Ctx.getPointerType(IdType));
3285
3286 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3287 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
3288 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
3289 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
3290 GcAssignStrongCastFn =
3291 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003292
3293 // void objc_exception_throw(id)
3294 Params.clear();
3295 Params.push_back(IdType);
3296
3297 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003298 ExceptionThrowFn =
3299 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00003300
3301 // synchronized APIs
Daniel Dunbar1c566672009-02-24 01:43:46 +00003302 // void objc_sync_exit (id)
3303 Params.clear();
3304 Params.push_back(IdType);
3305
3306 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003307 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003308}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003309
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003310ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3311 : ObjCCommonTypesHelper(cgm)
3312{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003313 // struct _objc_method_description {
3314 // SEL name;
3315 // char *types;
3316 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003317 MethodDescriptionTy =
3318 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003319 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003320 NULL);
3321 CGM.getModule().addTypeName("struct._objc_method_description",
3322 MethodDescriptionTy);
3323
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003324 // struct _objc_method_description_list {
3325 // int count;
3326 // struct _objc_method_description[1];
3327 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003328 MethodDescriptionListTy =
3329 llvm::StructType::get(IntTy,
3330 llvm::ArrayType::get(MethodDescriptionTy, 0),
3331 NULL);
3332 CGM.getModule().addTypeName("struct._objc_method_description_list",
3333 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003334
3335 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003336 MethodDescriptionListPtrTy =
3337 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3338
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003339 // Protocol description structures
3340
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003341 // struct _objc_protocol_extension {
3342 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3343 // struct _objc_method_description_list *optional_instance_methods;
3344 // struct _objc_method_description_list *optional_class_methods;
3345 // struct _objc_property_list *instance_properties;
3346 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003347 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003348 llvm::StructType::get(IntTy,
3349 MethodDescriptionListPtrTy,
3350 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003351 PropertyListPtrTy,
3352 NULL);
3353 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3354 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003355
3356 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003357 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3358
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003359 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003360
3361 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3362 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3363
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003364 const llvm::Type *T =
3365 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3366 LongTy,
3367 llvm::ArrayType::get(ProtocolTyHolder, 0),
3368 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003369 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3370
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003371 // struct _objc_protocol {
3372 // struct _objc_protocol_extension *isa;
3373 // char *protocol_name;
3374 // struct _objc_protocol **_objc_protocol_list;
3375 // struct _objc_method_description_list *instance_methods;
3376 // struct _objc_method_description_list *class_methods;
3377 // }
3378 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003379 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003380 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3381 MethodDescriptionListPtrTy,
3382 MethodDescriptionListPtrTy,
3383 NULL);
3384 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3385
3386 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3387 CGM.getModule().addTypeName("struct._objc_protocol_list",
3388 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003389 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003390 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3391
3392 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003393 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003394 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003395
3396 // Class description structures
3397
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003398 // struct _objc_ivar {
3399 // char *ivar_name;
3400 // char *ivar_type;
3401 // int ivar_offset;
3402 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003403 IvarTy = llvm::StructType::get(Int8PtrTy,
3404 Int8PtrTy,
3405 IntTy,
3406 NULL);
3407 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3408
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003409 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003410 IvarListTy = llvm::OpaqueType::get();
3411 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3412 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3413
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003414 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003415 MethodListTy = llvm::OpaqueType::get();
3416 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3417 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3418
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003419 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003420 ClassExtensionTy =
3421 llvm::StructType::get(IntTy,
3422 Int8PtrTy,
3423 PropertyListPtrTy,
3424 NULL);
3425 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3426 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3427
3428 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3429
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003430 // struct _objc_class {
3431 // Class isa;
3432 // Class super_class;
3433 // char *name;
3434 // long version;
3435 // long info;
3436 // long instance_size;
3437 // struct _objc_ivar_list *ivars;
3438 // struct _objc_method_list *methods;
3439 // struct _objc_cache *cache;
3440 // struct _objc_protocol_list *protocols;
3441 // char *ivar_layout;
3442 // struct _objc_class_ext *ext;
3443 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3445 llvm::PointerType::getUnqual(ClassTyHolder),
3446 Int8PtrTy,
3447 LongTy,
3448 LongTy,
3449 LongTy,
3450 IvarListPtrTy,
3451 MethodListPtrTy,
3452 CachePtrTy,
3453 ProtocolListPtrTy,
3454 Int8PtrTy,
3455 ClassExtensionPtrTy,
3456 NULL);
3457 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3458
3459 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3460 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3461 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3462
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003463 // struct _objc_category {
3464 // char *category_name;
3465 // char *class_name;
3466 // struct _objc_method_list *instance_method;
3467 // struct _objc_method_list *class_method;
3468 // uint32_t size; // sizeof(struct _objc_category)
3469 // struct _objc_property_list *instance_properties;// category's @property
3470 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003471 CategoryTy = llvm::StructType::get(Int8PtrTy,
3472 Int8PtrTy,
3473 MethodListPtrTy,
3474 MethodListPtrTy,
3475 ProtocolListPtrTy,
3476 IntTy,
3477 PropertyListPtrTy,
3478 NULL);
3479 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3480
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003481 // Global metadata structures
3482
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003483 // struct _objc_symtab {
3484 // long sel_ref_cnt;
3485 // SEL *refs;
3486 // short cls_def_cnt;
3487 // short cat_def_cnt;
3488 // char *defs[cls_def_cnt + cat_def_cnt];
3489 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003490 SymtabTy = llvm::StructType::get(LongTy,
3491 SelectorPtrTy,
3492 ShortTy,
3493 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003494 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003495 NULL);
3496 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3497 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3498
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003499 // struct _objc_module {
3500 // long version;
3501 // long size; // sizeof(struct _objc_module)
3502 // char *name;
3503 // struct _objc_symtab* symtab;
3504 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003505 ModuleTy =
3506 llvm::StructType::get(LongTy,
3507 LongTy,
3508 Int8PtrTy,
3509 SymtabPtrTy,
3510 NULL);
3511 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003512
Daniel Dunbar49f66022008-09-24 03:38:44 +00003513 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003514
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003515 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003516 std::vector<const llvm::Type*> Params;
3517 Params.push_back(ObjectPtrTy);
3518 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003519 MessageSendFn =
3520 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3521 Params,
3522 true),
3523 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003524
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003525 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003526 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003527 Params.push_back(ObjectPtrTy);
3528 Params.push_back(SelectorPtrTy);
3529 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003530 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3531 Params,
3532 true),
3533 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003534
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003535 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003536 Params.clear();
3537 Params.push_back(ObjectPtrTy);
3538 Params.push_back(SelectorPtrTy);
3539 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003540 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003541 MessageSendFpretFn =
3542 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3543 Params,
3544 true),
3545 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003546
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003547 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003548 Params.clear();
3549 Params.push_back(SuperPtrTy);
3550 Params.push_back(SelectorPtrTy);
3551 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003552 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3553 Params,
3554 true),
3555 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003556
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003557 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3558 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003559 Params.clear();
3560 Params.push_back(Int8PtrTy);
3561 Params.push_back(SuperPtrTy);
3562 Params.push_back(SelectorPtrTy);
3563 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003564 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3565 Params,
3566 true),
3567 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003568
3569 // There is no objc_msgSendSuper_fpret? How can that work?
3570 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003571
Anders Carlsson124526b2008-09-09 10:10:21 +00003572 // FIXME: This is the size of the setjmp buffer and should be
3573 // target specific. 18 is what's used on 32-bit X86.
3574 uint64_t SetJmpBufferSize = 18;
3575
3576 // Exceptions
3577 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003578 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003579
3580 ExceptionDataTy =
3581 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3582 SetJmpBufferSize),
3583 StackPtrTy, NULL);
3584 CGM.getModule().addTypeName("struct._objc_exception_data",
3585 ExceptionDataTy);
3586
3587 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003588 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3589 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003590 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3591 Params,
3592 false),
3593 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003594 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003595 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3596 Params,
3597 false),
3598 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003599 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003600 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3601 Params,
3602 false),
3603 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003604
3605 Params.clear();
3606 Params.push_back(ClassPtrTy);
3607 Params.push_back(ObjectPtrTy);
3608 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003609 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3610 Params,
3611 false),
3612 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003613
Anders Carlsson124526b2008-09-09 10:10:21 +00003614 Params.clear();
3615 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3616 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003617 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3618 Params,
3619 false),
3620 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003621
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003622}
3623
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003624ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003625: ObjCCommonTypesHelper(cgm)
3626{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003627 // struct _method_list_t {
3628 // uint32_t entsize; // sizeof(struct _objc_method)
3629 // uint32_t method_count;
3630 // struct _objc_method method_list[method_count];
3631 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003632 MethodListnfABITy = llvm::StructType::get(IntTy,
3633 IntTy,
3634 llvm::ArrayType::get(MethodTy, 0),
3635 NULL);
3636 CGM.getModule().addTypeName("struct.__method_list_t",
3637 MethodListnfABITy);
3638 // struct method_list_t *
3639 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003640
3641 // struct _protocol_t {
3642 // id isa; // NULL
3643 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003644 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003645 // const struct method_list_t * const instance_methods;
3646 // const struct method_list_t * const class_methods;
3647 // const struct method_list_t *optionalInstanceMethods;
3648 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003649 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003650 // const uint32_t size; // sizeof(struct _protocol_t)
3651 // const uint32_t flags; // = 0
3652 // }
3653
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003654 // Holder for struct _protocol_list_t *
3655 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3656
3657 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3658 Int8PtrTy,
3659 llvm::PointerType::getUnqual(
3660 ProtocolListTyHolder),
3661 MethodListnfABIPtrTy,
3662 MethodListnfABIPtrTy,
3663 MethodListnfABIPtrTy,
3664 MethodListnfABIPtrTy,
3665 PropertyListPtrTy,
3666 IntTy,
3667 IntTy,
3668 NULL);
3669 CGM.getModule().addTypeName("struct._protocol_t",
3670 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003671
3672 // struct _protocol_t*
3673 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003674
Fariborz Jahanianda320092009-01-29 19:24:30 +00003675 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003676 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003677 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003678 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003679 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3680 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003681 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003682 NULL);
3683 CGM.getModule().addTypeName("struct._objc_protocol_list",
3684 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003685 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3686 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003687
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003688 // struct _objc_protocol_list*
3689 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003690
3691 // struct _ivar_t {
3692 // unsigned long int *offset; // pointer to ivar offset location
3693 // char *name;
3694 // char *type;
3695 // uint32_t alignment;
3696 // uint32_t size;
3697 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003698 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3699 Int8PtrTy,
3700 Int8PtrTy,
3701 IntTy,
3702 IntTy,
3703 NULL);
3704 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3705
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003706 // struct _ivar_list_t {
3707 // uint32 entsize; // sizeof(struct _ivar_t)
3708 // uint32 count;
3709 // struct _iver_t list[count];
3710 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003711 IvarListnfABITy = llvm::StructType::get(IntTy,
3712 IntTy,
3713 llvm::ArrayType::get(
3714 IvarnfABITy, 0),
3715 NULL);
3716 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3717
3718 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003719
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003720 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003721 // uint32_t const flags;
3722 // uint32_t const instanceStart;
3723 // uint32_t const instanceSize;
3724 // uint32_t const reserved; // only when building for 64bit targets
3725 // const uint8_t * const ivarLayout;
3726 // const char *const name;
3727 // const struct _method_list_t * const baseMethods;
3728 // const struct _objc_protocol_list *const baseProtocols;
3729 // const struct _ivar_list_t *const ivars;
3730 // const uint8_t * const weakIvarLayout;
3731 // const struct _prop_list_t * const properties;
3732 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003733
3734 // FIXME. Add 'reserved' field in 64bit abi mode!
3735 ClassRonfABITy = llvm::StructType::get(IntTy,
3736 IntTy,
3737 IntTy,
3738 Int8PtrTy,
3739 Int8PtrTy,
3740 MethodListnfABIPtrTy,
3741 ProtocolListnfABIPtrTy,
3742 IvarListnfABIPtrTy,
3743 Int8PtrTy,
3744 PropertyListPtrTy,
3745 NULL);
3746 CGM.getModule().addTypeName("struct._class_ro_t",
3747 ClassRonfABITy);
3748
3749 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3750 std::vector<const llvm::Type*> Params;
3751 Params.push_back(ObjectPtrTy);
3752 Params.push_back(SelectorPtrTy);
3753 ImpnfABITy = llvm::PointerType::getUnqual(
3754 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3755
3756 // struct _class_t {
3757 // struct _class_t *isa;
3758 // struct _class_t * const superclass;
3759 // void *cache;
3760 // IMP *vtable;
3761 // struct class_ro_t *ro;
3762 // }
3763
3764 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3765 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3766 llvm::PointerType::getUnqual(ClassTyHolder),
3767 CachePtrTy,
3768 llvm::PointerType::getUnqual(ImpnfABITy),
3769 llvm::PointerType::getUnqual(
3770 ClassRonfABITy),
3771 NULL);
3772 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3773
3774 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3775 ClassnfABITy);
3776
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003777 // LLVM for struct _class_t *
3778 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3779
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003780 // struct _category_t {
3781 // const char * const name;
3782 // struct _class_t *const cls;
3783 // const struct _method_list_t * const instance_methods;
3784 // const struct _method_list_t * const class_methods;
3785 // const struct _protocol_list_t * const protocols;
3786 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003787 // }
3788 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003789 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003790 MethodListnfABIPtrTy,
3791 MethodListnfABIPtrTy,
3792 ProtocolListnfABIPtrTy,
3793 PropertyListPtrTy,
3794 NULL);
3795 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003796
3797 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003798 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3799 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003800
3801 // MessageRefTy - LLVM for:
3802 // struct _message_ref_t {
3803 // IMP messenger;
3804 // SEL name;
3805 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003806
3807 // First the clang type for struct _message_ref_t
3808 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3809 SourceLocation(),
3810 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003811 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3812 Ctx.VoidPtrTy, 0, false));
3813 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3814 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003815 RD->completeDefinition(Ctx);
3816
3817 MessageRefCTy = Ctx.getTagDeclType(RD);
3818 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3819 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003820
3821 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3822 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3823
3824 // SuperMessageRefTy - LLVM for:
3825 // struct _super_message_ref_t {
3826 // SUPER_IMP messenger;
3827 // SEL name;
3828 // };
3829 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3830 SelectorPtrTy,
3831 NULL);
3832 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3833
3834 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3835 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3836
3837 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3838 Params.clear();
3839 Params.push_back(ObjectPtrTy);
3840 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003841 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3842 Params,
3843 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003844 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003845 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003846 "objc_msgSend_fixup");
3847
3848 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3849 MessageSendFpretFixupFn =
3850 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3851 Params,
3852 true),
3853 "objc_msgSend_fpret_fixup");
3854
3855 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3856 MessageSendStretFixupFn =
3857 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3858 Params,
3859 true),
3860 "objc_msgSend_stret_fixup");
3861
3862 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3863 MessageSendIdFixupFn =
3864 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3865 Params,
3866 true),
3867 "objc_msgSendId_fixup");
3868
3869
3870 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3871 MessageSendIdStretFixupFn =
3872 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3873 Params,
3874 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003875 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003876
3877 // id objc_msgSendSuper2_fixup (struct objc_super *,
3878 // struct _super_message_ref_t*, ...)
3879 Params.clear();
3880 Params.push_back(SuperPtrTy);
3881 Params.push_back(SuperMessageRefPtrTy);
3882 MessageSendSuper2FixupFn =
3883 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3884 Params,
3885 true),
3886 "objc_msgSendSuper2_fixup");
3887
3888
3889 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3890 // struct _super_message_ref_t*, ...)
3891 MessageSendSuper2StretFixupFn =
3892 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3893 Params,
3894 true),
3895 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003896
3897 Params.clear();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003898 Params.push_back(Int8PtrTy);
3899 UnwindResumeOrRethrowFn =
3900 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3901 Params,
3902 false),
3903 "_Unwind_Resume_or_Rethrow");
Daniel Dunbare588b992009-03-01 04:46:24 +00003904 ObjCBeginCatchFn =
3905 CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
3906 Params,
3907 false),
3908 "objc_begin_catch");
3909
3910 Params.clear();
3911 ObjCEndCatchFn =
3912 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3913 Params,
3914 false),
3915 "objc_end_catch");
3916
3917 // struct objc_typeinfo {
3918 // const void** vtable; // objc_ehtype_vtable + 2
3919 // const char* name; // c++ typeinfo string
3920 // Class cls;
3921 // };
3922 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3923 Int8PtrTy,
3924 ClassnfABIPtrTy,
3925 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003926 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003927 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003928}
3929
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003930llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3931 FinishNonFragileABIModule();
3932
3933 return NULL;
3934}
3935
3936void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3937 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003938
3939 // Build list of all implemented classe addresses in array
3940 // L_OBJC_LABEL_CLASS_$.
3941 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3942 // list of 'nonlazy' implementations (defined as those with a +load{}
3943 // method!!).
3944 unsigned NumClasses = DefinedClasses.size();
3945 if (NumClasses) {
3946 std::vector<llvm::Constant*> Symbols(NumClasses);
3947 for (unsigned i=0; i<NumClasses; i++)
3948 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3949 ObjCTypes.Int8PtrTy);
3950 llvm::Constant* Init =
3951 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3952 NumClasses),
3953 Symbols);
3954
3955 llvm::GlobalVariable *GV =
3956 new llvm::GlobalVariable(Init->getType(), false,
3957 llvm::GlobalValue::InternalLinkage,
3958 Init,
3959 "\01L_OBJC_LABEL_CLASS_$",
3960 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003961 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003962 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3963 UsedGlobals.push_back(GV);
3964 }
3965
3966 // Build list of all implemented category addresses in array
3967 // L_OBJC_LABEL_CATEGORY_$.
3968 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3969 // list of 'nonlazy' category implementations (defined as those with a +load{}
3970 // method!!).
3971 unsigned NumCategory = DefinedCategories.size();
3972 if (NumCategory) {
3973 std::vector<llvm::Constant*> Symbols(NumCategory);
3974 for (unsigned i=0; i<NumCategory; i++)
3975 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3976 ObjCTypes.Int8PtrTy);
3977 llvm::Constant* Init =
3978 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3979 NumCategory),
3980 Symbols);
3981
3982 llvm::GlobalVariable *GV =
3983 new llvm::GlobalVariable(Init->getType(), false,
3984 llvm::GlobalValue::InternalLinkage,
3985 Init,
3986 "\01L_OBJC_LABEL_CATEGORY_$",
3987 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003988 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003989 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3990 UsedGlobals.push_back(GV);
3991 }
3992
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003993 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3994 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3995 std::vector<llvm::Constant*> Values(2);
3996 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003997 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00003998 // FIXME: Fix and continue?
3999 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4000 flags |= eImageInfo_GarbageCollected;
4001 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4002 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004003 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004004 llvm::Constant* Init = llvm::ConstantArray::get(
4005 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4006 Values);
4007 llvm::GlobalVariable *IMGV =
4008 new llvm::GlobalVariable(Init->getType(), false,
4009 llvm::GlobalValue::InternalLinkage,
4010 Init,
4011 "\01L_OBJC_IMAGE_INFO",
4012 &CGM.getModule());
4013 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
4014 UsedGlobals.push_back(IMGV);
4015
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004016 std::vector<llvm::Constant*> Used;
4017 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4018 e = UsedGlobals.end(); i != e; ++i) {
4019 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4020 }
4021
4022 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4023 llvm::GlobalValue *GV =
4024 new llvm::GlobalVariable(AT, false,
4025 llvm::GlobalValue::AppendingLinkage,
4026 llvm::ConstantArray::get(AT, Used),
4027 "llvm.used",
4028 &CGM.getModule());
4029
4030 GV->setSection("llvm.metadata");
4031
4032}
4033
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004034// Metadata flags
4035enum MetaDataDlags {
4036 CLS = 0x0,
4037 CLS_META = 0x1,
4038 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004039 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004040 CLS_EXCEPTION = 0x20
4041};
4042/// BuildClassRoTInitializer - generate meta-data for:
4043/// struct _class_ro_t {
4044/// uint32_t const flags;
4045/// uint32_t const instanceStart;
4046/// uint32_t const instanceSize;
4047/// uint32_t const reserved; // only when building for 64bit targets
4048/// const uint8_t * const ivarLayout;
4049/// const char *const name;
4050/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004051/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004052/// const struct _ivar_list_t *const ivars;
4053/// const uint8_t * const weakIvarLayout;
4054/// const struct _prop_list_t * const properties;
4055/// }
4056///
4057llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4058 unsigned flags,
4059 unsigned InstanceStart,
4060 unsigned InstanceSize,
4061 const ObjCImplementationDecl *ID) {
4062 std::string ClassName = ID->getNameAsString();
4063 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4064 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4065 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4066 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4067 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00004068 // FIXME. ivarLayout is currently null!
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004069 Values[ 3] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004070 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004071 // const struct _method_list_t * const baseMethods;
4072 std::vector<llvm::Constant*> Methods;
4073 std::string MethodListName("\01l_OBJC_$_");
4074 if (flags & CLS_META) {
4075 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4076 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
4077 e = ID->classmeth_end(); i != e; ++i) {
4078 // Class methods should always be defined.
4079 Methods.push_back(GetMethodConstant(*i));
4080 }
4081 } else {
4082 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4083 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
4084 e = ID->instmeth_end(); i != e; ++i) {
4085 // Instance methods should always be defined.
4086 Methods.push_back(GetMethodConstant(*i));
4087 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004088 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
4089 e = ID->propimpl_end(); i != e; ++i) {
4090 ObjCPropertyImplDecl *PID = *i;
4091
4092 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4093 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4094
4095 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4096 if (llvm::Constant *C = GetMethodConstant(MD))
4097 Methods.push_back(C);
4098 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4099 if (llvm::Constant *C = GetMethodConstant(MD))
4100 Methods.push_back(C);
4101 }
4102 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004103 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004104 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004105 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004106
4107 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4108 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4109 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4110 + OID->getNameAsString(),
4111 OID->protocol_begin(),
4112 OID->protocol_end());
4113
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004114 if (flags & CLS_META)
4115 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4116 else
4117 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004118 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004119 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004120 if (flags & CLS_META)
4121 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4122 else
4123 Values[ 9] =
4124 EmitPropertyList(
4125 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4126 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004127 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4128 Values);
4129 llvm::GlobalVariable *CLASS_RO_GV =
4130 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4131 llvm::GlobalValue::InternalLinkage,
4132 Init,
4133 (flags & CLS_META) ?
4134 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4135 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4136 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004137 CLASS_RO_GV->setAlignment(
4138 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004139 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004140 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004141
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004142}
4143
4144/// BuildClassMetaData - This routine defines that to-level meta-data
4145/// for the given ClassName for:
4146/// struct _class_t {
4147/// struct _class_t *isa;
4148/// struct _class_t * const superclass;
4149/// void *cache;
4150/// IMP *vtable;
4151/// struct class_ro_t *ro;
4152/// }
4153///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004154llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4155 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004156 llvm::Constant *IsAGV,
4157 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004158 llvm::Constant *ClassRoGV,
4159 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004160 std::vector<llvm::Constant*> Values(5);
4161 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004162 Values[1] = SuperClassGV
4163 ? SuperClassGV
4164 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004165 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4166 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4167 Values[4] = ClassRoGV; // &CLASS_RO_GV
4168 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4169 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004170 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4171 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004172 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004173 GV->setAlignment(
4174 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004175 if (HiddenVisibility)
4176 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004177 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004178}
4179
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004180void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4181 std::string ClassName = ID->getNameAsString();
4182 if (!ObjCEmptyCacheVar) {
4183 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004184 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004185 false,
4186 llvm::GlobalValue::ExternalLinkage,
4187 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004188 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004189 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004190
4191 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004192 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004193 false,
4194 llvm::GlobalValue::ExternalLinkage,
4195 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004196 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004197 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004198 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004199 assert(ID->getClassInterface() &&
4200 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004201 uint32_t InstanceStart =
4202 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4203 uint32_t InstanceSize = InstanceStart;
4204 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004205 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4206 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004207
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004208 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004209
Daniel Dunbar04d40782009-04-14 06:00:08 +00004210 bool classIsHidden =
4211 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004212 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004213 flags |= OBJC2_CLS_HIDDEN;
4214 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004215 // class is root
4216 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004217 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004218 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName, false);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004219 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004220 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004221 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4222 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4223 Root = Super;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004224 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString(), false);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004225 // work on super class metadata symbol.
4226 std::string SuperClassName =
4227 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004228 SuperClassGV = GetClassGlobal(SuperClassName, false);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004229 }
4230 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4231 InstanceStart,
4232 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004233 std::string TClassName = ObjCMetaClassName + ClassName;
4234 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004235 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4236 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004237
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004238 // Metadata for the class
4239 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004240 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004241 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004242
4243 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4244 flags |= CLS_EXCEPTION;
4245
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004246 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004247 flags |= CLS_ROOT;
4248 SuperClassGV = 0;
4249 }
4250 else {
4251 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004252 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004253 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004254 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004255 }
Fariborz Jahanianebf9ed32009-03-20 20:48:19 +00004256 // FIXME: Gross
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004257 InstanceStart = InstanceSize = 0;
4258 if (ObjCInterfaceDecl *OID =
4259 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
4260 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004261 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004262
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004263 RecordDecl::field_iterator firstField, lastField;
4264 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004265
Douglas Gregor6ab35242009-04-09 21:40:53 +00004266 for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()),
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004267 ifield = firstField; ifield != e; ++ifield)
4268 lastField = ifield;
4269
Douglas Gregor6ab35242009-04-09 21:40:53 +00004270 if (lastField != RD->field_end(CGM.getContext())) {
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004271 FieldDecl *Field = *lastField;
4272 const llvm::Type *FieldTy =
4273 CGM.getTypes().ConvertTypeForMem(Field->getType());
4274 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004275 InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004276 if (firstField == RD->field_end(CGM.getContext()))
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004277 InstanceStart = InstanceSize;
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004278 else {
4279 Field = *firstField;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004280 InstanceStart = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004281 }
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004282 }
4283 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004284 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004285 InstanceStart,
4286 InstanceSize,
4287 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004288
4289 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004290 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004291 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4292 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004293 UsedGlobals.push_back(ClassMD);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004294 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004295
4296 // Force the definition of the EHType if necessary.
4297 if (flags & CLS_EXCEPTION)
4298 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004299}
4300
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004301/// GenerateProtocolRef - This routine is called to generate code for
4302/// a protocol reference expression; as in:
4303/// @code
4304/// @protocol(Proto1);
4305/// @endcode
4306/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4307/// which will hold address of the protocol meta-data.
4308///
4309llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4310 const ObjCProtocolDecl *PD) {
4311
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004312 // This routine is called for @protocol only. So, we must build definition
4313 // of protocol's meta-data (not a reference to it!)
4314 //
4315 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004316 ObjCTypes.ExternalProtocolPtrTy);
4317
4318 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4319 ProtocolName += PD->getNameAsCString();
4320
4321 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4322 if (PTGV)
4323 return Builder.CreateLoad(PTGV, false, "tmp");
4324 PTGV = new llvm::GlobalVariable(
4325 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004326 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004327 Init,
4328 ProtocolName,
4329 &CGM.getModule());
4330 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4331 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4332 UsedGlobals.push_back(PTGV);
4333 return Builder.CreateLoad(PTGV, false, "tmp");
4334}
4335
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004336/// GenerateCategory - Build metadata for a category implementation.
4337/// struct _category_t {
4338/// const char * const name;
4339/// struct _class_t *const cls;
4340/// const struct _method_list_t * const instance_methods;
4341/// const struct _method_list_t * const class_methods;
4342/// const struct _protocol_list_t * const protocols;
4343/// const struct _prop_list_t * const properties;
4344/// }
4345///
4346void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4347{
4348 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004349 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4350 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004351 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004352 std::string ExtClassName(getClassSymbolPrefix() +
4353 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004354
4355 std::vector<llvm::Constant*> Values(6);
4356 Values[0] = GetClassName(OCD->getIdentifier());
4357 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004358 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004359 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004360 std::vector<llvm::Constant*> Methods;
4361 std::string MethodListName(Prefix);
4362 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4363 "_$_" + OCD->getNameAsString();
4364
4365 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
4366 e = OCD->instmeth_end(); i != e; ++i) {
4367 // Instance methods should always be defined.
4368 Methods.push_back(GetMethodConstant(*i));
4369 }
4370
4371 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004372 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004373 Methods);
4374
4375 MethodListName = Prefix;
4376 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4377 OCD->getNameAsString();
4378 Methods.clear();
4379 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
4380 e = OCD->classmeth_end(); i != e; ++i) {
4381 // Class methods should always be defined.
4382 Methods.push_back(GetMethodConstant(*i));
4383 }
4384
4385 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004386 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004387 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004388 const ObjCCategoryDecl *Category =
4389 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004390 if (Category) {
4391 std::string ExtName(Interface->getNameAsString() + "_$_" +
4392 OCD->getNameAsString());
4393 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4394 + Interface->getNameAsString() + "_$_"
4395 + Category->getNameAsString(),
4396 Category->protocol_begin(),
4397 Category->protocol_end());
4398 Values[5] =
4399 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4400 OCD, Category, ObjCTypes);
4401 }
4402 else {
4403 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4404 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4405 }
4406
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004407 llvm::Constant *Init =
4408 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4409 Values);
4410 llvm::GlobalVariable *GCATV
4411 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4412 false,
4413 llvm::GlobalValue::InternalLinkage,
4414 Init,
4415 ExtCatName,
4416 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004417 GCATV->setAlignment(
4418 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004419 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004420 UsedGlobals.push_back(GCATV);
4421 DefinedCategories.push_back(GCATV);
4422}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004423
4424/// GetMethodConstant - Return a struct objc_method constant for the
4425/// given method if it has been defined. The result is null if the
4426/// method has not been defined. The return value has type MethodPtrTy.
4427llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4428 const ObjCMethodDecl *MD) {
4429 // FIXME: Use DenseMap::lookup
4430 llvm::Function *Fn = MethodDefinitions[MD];
4431 if (!Fn)
4432 return 0;
4433
4434 std::vector<llvm::Constant*> Method(3);
4435 Method[0] =
4436 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4437 ObjCTypes.SelectorPtrTy);
4438 Method[1] = GetMethodVarType(MD);
4439 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4440 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4441}
4442
4443/// EmitMethodList - Build meta-data for method declarations
4444/// struct _method_list_t {
4445/// uint32_t entsize; // sizeof(struct _objc_method)
4446/// uint32_t method_count;
4447/// struct _objc_method method_list[method_count];
4448/// }
4449///
4450llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4451 const std::string &Name,
4452 const char *Section,
4453 const ConstantVector &Methods) {
4454 // Return null for empty list.
4455 if (Methods.empty())
4456 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4457
4458 std::vector<llvm::Constant*> Values(3);
4459 // sizeof(struct _objc_method)
4460 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4461 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4462 // method_count
4463 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4464 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4465 Methods.size());
4466 Values[2] = llvm::ConstantArray::get(AT, Methods);
4467 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4468
4469 llvm::GlobalVariable *GV =
4470 new llvm::GlobalVariable(Init->getType(), false,
4471 llvm::GlobalValue::InternalLinkage,
4472 Init,
4473 Name,
4474 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004475 GV->setAlignment(
4476 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004477 GV->setSection(Section);
4478 UsedGlobals.push_back(GV);
4479 return llvm::ConstantExpr::getBitCast(GV,
4480 ObjCTypes.MethodListnfABIPtrTy);
4481}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004482
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004483/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4484/// the given ivar.
4485///
4486llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
4487 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004488 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004489 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004490 Name += "\01_OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004491 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4492 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004493 llvm::GlobalVariable *IvarOffsetGV =
4494 CGM.getModule().getGlobalVariable(Name);
4495 if (!IvarOffsetGV)
4496 IvarOffsetGV =
4497 new llvm::GlobalVariable(ObjCTypes.LongTy,
4498 false,
4499 llvm::GlobalValue::ExternalLinkage,
4500 0,
4501 Name,
4502 &CGM.getModule());
4503 return IvarOffsetGV;
4504}
4505
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004506llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004507 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004508 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004509 unsigned long int Offset) {
4510
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004511 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004512 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004513 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004514 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004515 llvm::GlobalVariable *IvarOffsetGV =
4516 CGM.getModule().getGlobalVariable(ExternalName);
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004517 if (IvarOffsetGV)
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004518 // ivar offset symbol already built due to user code referencing it.
4519 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004520 else
4521 IvarOffsetGV =
4522 new llvm::GlobalVariable(Init->getType(),
4523 false,
4524 llvm::GlobalValue::ExternalLinkage,
4525 Init,
4526 ExternalName,
4527 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004528 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004529 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004530 // @private and @package have hidden visibility.
4531 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
Daniel Dunbar04d40782009-04-14 06:00:08 +00004532 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4533 if (!globalVisibility || CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004534 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004535 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004536 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004537 IvarOffsetGV->setSection("__DATA, __objc_const");
4538 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004539 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004540}
4541
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004542/// EmitIvarList - Emit the ivar list for the given
4543/// implementation. If ForClass is true the list of class ivars
4544/// (i.e. metaclass ivars) is emitted, otherwise the list of
4545/// interface ivars will be emitted. The return value has type
4546/// IvarListnfABIPtrTy.
4547/// struct _ivar_t {
4548/// unsigned long int *offset; // pointer to ivar offset location
4549/// char *name;
4550/// char *type;
4551/// uint32_t alignment;
4552/// uint32_t size;
4553/// }
4554/// struct _ivar_list_t {
4555/// uint32 entsize; // sizeof(struct _ivar_t)
4556/// uint32 count;
4557/// struct _iver_t list[count];
4558/// }
4559///
4560llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4561 const ObjCImplementationDecl *ID) {
4562
4563 std::vector<llvm::Constant*> Ivars, Ivar(5);
4564
4565 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4566 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4567
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004568 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004569 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004570
4571 RecordDecl::field_iterator i,p;
4572 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian18191882009-03-31 18:11:23 +00004573 // collect declared and synthesized ivars in a small vector.
4574 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
4575 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4576 E = OID->ivar_end(); I != E; ++I)
4577 OIvars.push_back(*I);
Douglas Gregor6ab35242009-04-09 21:40:53 +00004578 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4579 E = OID->prop_end(CGM.getContext()); I != E; ++I)
Fariborz Jahanian18191882009-03-31 18:11:23 +00004580 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4581 OIvars.push_back(IV);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004582
Fariborz Jahanian18191882009-03-31 18:11:23 +00004583 unsigned iv = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004584 for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext());
4585 i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004586 FieldDecl *Field = *i;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004587 uint64_t offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian18191882009-03-31 18:11:23 +00004588 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004589 if (Field->getIdentifier())
4590 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4591 else
4592 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00004593 Ivar[2] = GetMethodVarType(Field);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004594 const llvm::Type *FieldTy =
4595 CGM.getTypes().ConvertTypeForMem(Field->getType());
4596 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4597 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4598 Field->getType().getTypePtr()) >> 3;
4599 Align = llvm::Log2_32(Align);
4600 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004601 // NOTE. Size of a bitfield does not match gcc's, because of the way
4602 // bitfields are treated special in each. But I am told that 'size'
4603 // for bitfield ivars is ignored by the runtime so it does not matter.
4604 // (even if it matters, some day, there is enough info. to get the bitfield
4605 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004606 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4607 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4608 }
4609 // Return null for empty list.
4610 if (Ivars.empty())
4611 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4612 std::vector<llvm::Constant*> Values(3);
4613 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4614 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4615 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4616 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4617 Ivars.size());
4618 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4619 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4620 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4621 llvm::GlobalVariable *GV =
4622 new llvm::GlobalVariable(Init->getType(), false,
4623 llvm::GlobalValue::InternalLinkage,
4624 Init,
4625 Prefix + OID->getNameAsString(),
4626 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004627 GV->setAlignment(
4628 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004629 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004630
4631 UsedGlobals.push_back(GV);
4632 return llvm::ConstantExpr::getBitCast(GV,
4633 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004634}
4635
4636llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4637 const ObjCProtocolDecl *PD) {
4638 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4639
4640 if (!Entry) {
4641 // We use the initializer as a marker of whether this is a forward
4642 // reference or not. At module finalization we add the empty
4643 // contents for protocols which were referenced but never defined.
4644 Entry =
4645 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4646 llvm::GlobalValue::ExternalLinkage,
4647 0,
4648 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4649 &CGM.getModule());
4650 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4651 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004652 }
4653
4654 return Entry;
4655}
4656
4657/// GetOrEmitProtocol - Generate the protocol meta-data:
4658/// @code
4659/// struct _protocol_t {
4660/// id isa; // NULL
4661/// const char * const protocol_name;
4662/// const struct _protocol_list_t * protocol_list; // super protocols
4663/// const struct method_list_t * const instance_methods;
4664/// const struct method_list_t * const class_methods;
4665/// const struct method_list_t *optionalInstanceMethods;
4666/// const struct method_list_t *optionalClassMethods;
4667/// const struct _prop_list_t * properties;
4668/// const uint32_t size; // sizeof(struct _protocol_t)
4669/// const uint32_t flags; // = 0
4670/// }
4671/// @endcode
4672///
4673
4674llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4675 const ObjCProtocolDecl *PD) {
4676 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4677
4678 // Early exit if a defining object has already been generated.
4679 if (Entry && Entry->hasInitializer())
4680 return Entry;
4681
4682 const char *ProtocolName = PD->getNameAsCString();
4683
4684 // Construct method lists.
4685 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4686 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004687 for (ObjCProtocolDecl::instmeth_iterator
4688 i = PD->instmeth_begin(CGM.getContext()),
4689 e = PD->instmeth_end(CGM.getContext());
4690 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004691 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004692 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004693 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4694 OptInstanceMethods.push_back(C);
4695 } else {
4696 InstanceMethods.push_back(C);
4697 }
4698 }
4699
Douglas Gregor6ab35242009-04-09 21:40:53 +00004700 for (ObjCProtocolDecl::classmeth_iterator
4701 i = PD->classmeth_begin(CGM.getContext()),
4702 e = PD->classmeth_end(CGM.getContext());
4703 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004704 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004705 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004706 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4707 OptClassMethods.push_back(C);
4708 } else {
4709 ClassMethods.push_back(C);
4710 }
4711 }
4712
4713 std::vector<llvm::Constant*> Values(10);
4714 // isa is NULL
4715 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4716 Values[1] = GetClassName(PD->getIdentifier());
4717 Values[2] = EmitProtocolList(
4718 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4719 PD->protocol_begin(),
4720 PD->protocol_end());
4721
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004722 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004723 + PD->getNameAsString(),
4724 "__DATA, __objc_const",
4725 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004726 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004727 + PD->getNameAsString(),
4728 "__DATA, __objc_const",
4729 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004730 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004731 + PD->getNameAsString(),
4732 "__DATA, __objc_const",
4733 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004734 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004735 + PD->getNameAsString(),
4736 "__DATA, __objc_const",
4737 OptClassMethods);
4738 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4739 0, PD, ObjCTypes);
4740 uint32_t Size =
4741 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4742 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4743 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4744 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4745 Values);
4746
4747 if (Entry) {
4748 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004749 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004750 Entry->setInitializer(Init);
4751 } else {
4752 Entry =
4753 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004754 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004755 Init,
4756 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4757 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004758 Entry->setAlignment(
4759 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004760 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004761 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004762 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4763
4764 // Use this protocol meta-data to build protocol list table in section
4765 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004766 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004767 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004768 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004769 Entry,
4770 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4771 +ProtocolName,
4772 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004773 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004774 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004775 PTGV->setSection("__DATA, __objc_protolist");
4776 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4777 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004778 return Entry;
4779}
4780
4781/// EmitProtocolList - Generate protocol list meta-data:
4782/// @code
4783/// struct _protocol_list_t {
4784/// long protocol_count; // Note, this is 32/64 bit
4785/// struct _protocol_t[protocol_count];
4786/// }
4787/// @endcode
4788///
4789llvm::Constant *
4790CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4791 ObjCProtocolDecl::protocol_iterator begin,
4792 ObjCProtocolDecl::protocol_iterator end) {
4793 std::vector<llvm::Constant*> ProtocolRefs;
4794
Fariborz Jahanianda320092009-01-29 19:24:30 +00004795 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004796 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004797 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4798
Daniel Dunbar948e2582009-02-15 07:36:20 +00004799 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004800 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4801 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004802 return llvm::ConstantExpr::getBitCast(GV,
4803 ObjCTypes.ProtocolListnfABIPtrTy);
4804
4805 for (; begin != end; ++begin)
4806 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4807
Fariborz Jahanianda320092009-01-29 19:24:30 +00004808 // This list is null terminated.
4809 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004810 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004811
4812 std::vector<llvm::Constant*> Values(2);
4813 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4814 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004815 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004816 ProtocolRefs.size()),
4817 ProtocolRefs);
4818
4819 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4820 GV = new llvm::GlobalVariable(Init->getType(), false,
4821 llvm::GlobalValue::InternalLinkage,
4822 Init,
4823 Name,
4824 &CGM.getModule());
4825 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004826 GV->setAlignment(
4827 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004828 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004829 return llvm::ConstantExpr::getBitCast(GV,
4830 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004831}
4832
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004833/// GetMethodDescriptionConstant - This routine build following meta-data:
4834/// struct _objc_method {
4835/// SEL _cmd;
4836/// char *method_type;
4837/// char *_imp;
4838/// }
4839
4840llvm::Constant *
4841CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4842 std::vector<llvm::Constant*> Desc(3);
4843 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4844 ObjCTypes.SelectorPtrTy);
4845 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004846 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004847 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4848 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4849}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004850
4851/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4852/// This code gen. amounts to generating code for:
4853/// @code
4854/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4855/// @encode
4856///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004857LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004858 CodeGen::CodeGenFunction &CGF,
4859 QualType ObjectTy,
4860 llvm::Value *BaseValue,
4861 const ObjCIvarDecl *Ivar,
4862 const FieldDecl *Field,
4863 unsigned CVRQualifiers) {
4864 assert(ObjectTy->isObjCInterfaceType() &&
4865 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004866 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004867 std::string ExternalName;
4868 llvm::GlobalVariable *IvarOffsetGV =
4869 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004870
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004871 // (char *) BaseValue
4872 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4873 ObjCTypes.Int8PtrTy);
4874 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4875 // (char*)BaseValue + Offset_symbol
4876 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4877 // (type *)((char*)BaseValue + Offset_symbol)
4878 const llvm::Type *IvarTy =
4879 CGM.getTypes().ConvertType(Ivar->getType());
4880 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4881 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004882
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004883 if (Ivar->isBitField()) {
4884 CodeGenTypes::BitFieldInfo bitFieldInfo =
4885 CGM.getTypes().getBitFieldInfo(Field);
4886 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
4887 Field->getType()->isSignedIntegerType(),
4888 Field->getType().getCVRQualifiers()|CVRQualifiers);
4889 }
4890
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004891 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004892 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4893 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004894 LValue::SetObjCIvar(LV, true);
4895 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004896}
4897
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004898llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4899 CodeGen::CodeGenFunction &CGF,
4900 ObjCInterfaceDecl *Interface,
4901 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004902 std::string ExternalName;
4903 llvm::GlobalVariable *IvarOffsetGV =
4904 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4905
4906 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004907}
4908
Fariborz Jahanian46551122009-02-04 00:22:57 +00004909CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4910 CodeGen::CodeGenFunction &CGF,
4911 QualType ResultType,
4912 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004913 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004914 QualType Arg0Ty,
4915 bool IsSuper,
4916 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004917 // FIXME. Even though IsSuper is passes. This function doese not
4918 // handle calls to 'super' receivers.
4919 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004920 llvm::Value *Arg0 = Receiver;
4921 if (!IsSuper)
4922 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004923
4924 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004925 // FIXME. This is too much work to get the ABI-specific result type
4926 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004927 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4928 llvm::SmallVector<QualType, 16>());
4929 llvm::Constant *Fn;
4930 std::string Name("\01l_");
4931 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004932#if 0
4933 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004934 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4935 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4936 // FIXME. Is there a better way of getting these names.
4937 // They are available in RuntimeFunctions vector pair.
4938 Name += "objc_msgSendId_stret_fixup";
4939 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004940 else
4941#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004942 if (IsSuper) {
4943 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4944 Name += "objc_msgSendSuper2_stret_fixup";
4945 }
4946 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004947 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004948 Fn = ObjCTypes.MessageSendStretFixupFn;
4949 Name += "objc_msgSend_stret_fixup";
4950 }
4951 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004952 else if (ResultType->isFloatingType() &&
4953 // Selection of frret API only happens in 32bit nonfragile ABI.
4954 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004955 Fn = ObjCTypes.MessageSendFpretFixupFn;
4956 Name += "objc_msgSend_fpret_fixup";
4957 }
4958 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004959#if 0
4960// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004961 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4962 Fn = ObjCTypes.MessageSendIdFixupFn;
4963 Name += "objc_msgSendId_fixup";
4964 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004965 else
4966#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004967 if (IsSuper) {
4968 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4969 Name += "objc_msgSendSuper2_fixup";
4970 }
4971 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004972 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004973 Fn = ObjCTypes.MessageSendFixupFn;
4974 Name += "objc_msgSend_fixup";
4975 }
4976 }
4977 Name += '_';
4978 std::string SelName(Sel.getAsString());
4979 // Replace all ':' in selector name with '_' ouch!
4980 for(unsigned i = 0; i < SelName.size(); i++)
4981 if (SelName[i] == ':')
4982 SelName[i] = '_';
4983 Name += SelName;
4984 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4985 if (!GV) {
4986 // Build messafe ref table entry.
4987 std::vector<llvm::Constant*> Values(2);
4988 Values[0] = Fn;
4989 Values[1] = GetMethodVarName(Sel);
4990 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4991 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004992 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004993 Init,
4994 Name,
4995 &CGM.getModule());
4996 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4997 GV->setAlignment(
4998 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4999 GV->setSection("__DATA, __objc_msgrefs, coalesced");
5000 UsedGlobals.push_back(GV);
5001 }
5002 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005003
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005004 CallArgList ActualArgs;
5005 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5006 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5007 ObjCTypes.MessageRefCPtrTy));
5008 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005009 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5010 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5011 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005012 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005013 Callee = CGF.Builder.CreateBitCast(Callee,
5014 llvm::PointerType::getUnqual(FTy));
5015 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005016}
5017
5018/// Generate code for a message send expression in the nonfragile abi.
5019CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5020 CodeGen::CodeGenFunction &CGF,
5021 QualType ResultType,
5022 Selector Sel,
5023 llvm::Value *Receiver,
5024 bool IsClassMessage,
5025 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005026 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005027 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005028 false, CallArgs);
5029}
5030
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005031llvm::GlobalVariable *
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005032CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name,
5033 bool AddToUsed) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005034 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5035
Daniel Dunbardfff2302009-03-02 05:18:14 +00005036 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005037 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5038 llvm::GlobalValue::ExternalLinkage,
5039 0, Name, &CGM.getModule());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005040 if (AddToUsed)
5041 UsedGlobals.push_back(GV);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005042 }
5043
5044 return GV;
5045}
5046
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005047llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005048 const ObjCInterfaceDecl *ID,
5049 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005050
5051 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5052
5053 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005054 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005055 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005056 Entry =
5057 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5058 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005059 ClassGV,
5060 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
5061 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005062 &CGM.getModule());
5063 Entry->setAlignment(
5064 CGM.getTargetData().getPrefTypeAlignment(
5065 ObjCTypes.ClassnfABIPtrTy));
5066
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005067 if (IsSuper)
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005068 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005069 else
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005070 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005071 UsedGlobals.push_back(Entry);
5072 }
5073
5074 return Builder.CreateLoad(Entry, false, "tmp");
5075}
5076
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005077/// EmitMetaClassRef - Return a Value * of the address of _class_t
5078/// meta-data
5079///
5080llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5081 const ObjCInterfaceDecl *ID) {
5082 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5083 if (Entry)
5084 return Builder.CreateLoad(Entry, false, "tmp");
5085
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005086 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
5087 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName, false);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005088 Entry =
5089 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5090 llvm::GlobalValue::InternalLinkage,
5091 MetaClassGV,
5092 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5093 &CGM.getModule());
5094 Entry->setAlignment(
5095 CGM.getTargetData().getPrefTypeAlignment(
5096 ObjCTypes.ClassnfABIPtrTy));
5097
5098 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
5099 UsedGlobals.push_back(Entry);
5100
5101 return Builder.CreateLoad(Entry, false, "tmp");
5102}
5103
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005104/// GetClass - Return a reference to the class for the given interface
5105/// decl.
5106llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5107 const ObjCInterfaceDecl *ID) {
5108 return EmitClassRef(Builder, ID);
5109}
5110
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005111/// Generates a message send where the super is the receiver. This is
5112/// a message send to self with special delivery semantics indicating
5113/// which class's method should be called.
5114CodeGen::RValue
5115CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5116 QualType ResultType,
5117 Selector Sel,
5118 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005119 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005120 llvm::Value *Receiver,
5121 bool IsClassMessage,
5122 const CodeGen::CallArgList &CallArgs) {
5123 // ...
5124 // Create and init a super structure; this is a (receiver, class)
5125 // pair we will pass to objc_msgSendSuper.
5126 llvm::Value *ObjCSuper =
5127 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5128
5129 llvm::Value *ReceiverAsObject =
5130 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5131 CGF.Builder.CreateStore(ReceiverAsObject,
5132 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5133
5134 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005135 llvm::Value *Target;
5136 if (IsClassMessage) {
5137 if (isCategoryImpl) {
5138 // Message sent to "super' in a class method defined in
5139 // a category implementation.
5140 Target = EmitClassRef(CGF.Builder, Class, false);
5141 Target = CGF.Builder.CreateStructGEP(Target, 0);
5142 Target = CGF.Builder.CreateLoad(Target);
5143 }
5144 else
5145 Target = EmitMetaClassRef(CGF.Builder, Class);
5146 }
5147 else
5148 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005149
5150 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5151 // and ObjCTypes types.
5152 const llvm::Type *ClassTy =
5153 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5154 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5155 CGF.Builder.CreateStore(Target,
5156 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5157
5158 return EmitMessageSend(CGF, ResultType, Sel,
5159 ObjCSuper, ObjCTypes.SuperPtrCTy,
5160 true, CallArgs);
5161}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005162
5163llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5164 Selector Sel) {
5165 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5166
5167 if (!Entry) {
5168 llvm::Constant *Casted =
5169 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5170 ObjCTypes.SelectorPtrTy);
5171 Entry =
5172 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5173 llvm::GlobalValue::InternalLinkage,
5174 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5175 &CGM.getModule());
5176 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5177 UsedGlobals.push_back(Entry);
5178 }
5179
5180 return Builder.CreateLoad(Entry, false, "tmp");
5181}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005182/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5183/// objc_assign_ivar (id src, id *dst)
5184///
5185void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5186 llvm::Value *src, llvm::Value *dst)
5187{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005188 const llvm::Type * SrcTy = src->getType();
5189 if (!isa<llvm::PointerType>(SrcTy)) {
5190 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5191 assert(Size <= 8 && "does not support size > 8");
5192 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5193 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005194 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5195 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005196 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5197 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5198 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
5199 src, dst, "assignivar");
5200 return;
5201}
5202
5203/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5204/// objc_assign_strongCast (id src, id *dst)
5205///
5206void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5207 CodeGen::CodeGenFunction &CGF,
5208 llvm::Value *src, llvm::Value *dst)
5209{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005210 const llvm::Type * SrcTy = src->getType();
5211 if (!isa<llvm::PointerType>(SrcTy)) {
5212 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5213 assert(Size <= 8 && "does not support size > 8");
5214 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5215 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005216 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5217 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005218 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5219 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5220 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
5221 src, dst, "weakassign");
5222 return;
5223}
5224
5225/// EmitObjCWeakRead - Code gen for loading value of a __weak
5226/// object: objc_read_weak (id *src)
5227///
5228llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5229 CodeGen::CodeGenFunction &CGF,
5230 llvm::Value *AddrWeakObj)
5231{
Eli Friedman8339b352009-03-07 03:57:15 +00005232 const llvm::Type* DestTy =
5233 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005234 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
5235 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
5236 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005237 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005238 return read_weak;
5239}
5240
5241/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5242/// objc_assign_weak (id src, id *dst)
5243///
5244void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5245 llvm::Value *src, llvm::Value *dst)
5246{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005247 const llvm::Type * SrcTy = src->getType();
5248 if (!isa<llvm::PointerType>(SrcTy)) {
5249 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5250 assert(Size <= 8 && "does not support size > 8");
5251 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5252 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005253 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5254 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005255 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5256 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5257 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
5258 src, dst, "weakassign");
5259 return;
5260}
5261
5262/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5263/// objc_assign_global (id src, id *dst)
5264///
5265void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5266 llvm::Value *src, llvm::Value *dst)
5267{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005268 const llvm::Type * SrcTy = src->getType();
5269 if (!isa<llvm::PointerType>(SrcTy)) {
5270 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5271 assert(Size <= 8 && "does not support size > 8");
5272 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5273 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005274 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5275 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005276 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5277 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5278 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
5279 src, dst, "globalassign");
5280 return;
5281}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005282
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005283void
5284CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5285 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005286 bool isTry = isa<ObjCAtTryStmt>(S);
5287 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5288 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005289 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005290 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005291 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005292 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5293
5294 // For @synchronized, call objc_sync_enter(sync.expr). The
5295 // evaluation of the expression must occur before we enter the
5296 // @synchronized. We can safely avoid a temp here because jumps into
5297 // @synchronized are illegal & this will dominate uses.
5298 llvm::Value *SyncArg = 0;
5299 if (!isTry) {
5300 SyncArg =
5301 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5302 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005303 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005304 }
5305
5306 // Push an EH context entry, used for handling rethrows and jumps
5307 // through finally.
5308 CGF.PushCleanupBlock(FinallyBlock);
5309
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005310 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005311
5312 CGF.EmitBlock(TryBlock);
5313 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5314 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5315 CGF.EmitBranchThroughCleanup(FinallyEnd);
5316
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005317 // Emit the exception handler.
5318
5319 CGF.EmitBlock(TryHandler);
5320
5321 llvm::Value *llvm_eh_exception =
5322 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5323 llvm::Value *llvm_eh_selector_i64 =
5324 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5325 llvm::Value *llvm_eh_typeid_for_i64 =
5326 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5327 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5328 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5329
5330 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5331 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005332 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005333
5334 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005335 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005336 bool HasCatchAll = false;
5337 if (isTry) {
5338 if (const ObjCAtCatchStmt* CatchStmt =
5339 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5340 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005341 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005342 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005343
5344 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005345 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005346 // Use i8* null here to signal this is a catch all, not a cleanup.
5347 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5348 SelectorArgs.push_back(Null);
5349 HasCatchAll = true;
5350 break;
5351 }
5352
Daniel Dunbarede8de92009-03-06 00:01:21 +00005353 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5354 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005355 llvm::Value *IDEHType =
5356 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5357 if (!IDEHType)
5358 IDEHType =
5359 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5360 llvm::GlobalValue::ExternalLinkage,
5361 0, "OBJC_EHTYPE_id", &CGM.getModule());
5362 SelectorArgs.push_back(IDEHType);
5363 HasCatchAll = true;
5364 break;
5365 }
5366
5367 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005368 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005369 assert(PT && "Invalid @catch type.");
5370 const ObjCInterfaceType *IT =
5371 PT->getPointeeType()->getAsObjCInterfaceType();
5372 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005373 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005374 SelectorArgs.push_back(EHType);
5375 }
5376 }
5377 }
5378
5379 // We use a cleanup unless there was already a catch all.
5380 if (!HasCatchAll) {
5381 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005382 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005383 }
5384
5385 llvm::Value *Selector =
5386 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5387 SelectorArgs.begin(), SelectorArgs.end(),
5388 "selector");
5389 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005390 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005391 const Stmt *CatchBody = Handlers[i].second;
5392
5393 llvm::BasicBlock *Next = 0;
5394
5395 // The last handler always matches.
5396 if (i + 1 != e) {
5397 assert(CatchParam && "Only last handler can be a catch all.");
5398
5399 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5400 Next = CGF.createBasicBlock("catch.next");
5401 llvm::Value *Id =
5402 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5403 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5404 ObjCTypes.Int8PtrTy));
5405 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5406 Match, Next);
5407
5408 CGF.EmitBlock(Match);
5409 }
5410
5411 if (CatchBody) {
5412 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5413 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5414
5415 // Cleanups must call objc_end_catch.
5416 //
5417 // FIXME: It seems incorrect for objc_begin_catch to be inside
5418 // this context, but this matches gcc.
5419 CGF.PushCleanupBlock(MatchEnd);
5420 CGF.setInvokeDest(MatchHandler);
5421
5422 llvm::Value *ExcObject =
5423 CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc);
5424
5425 // Bind the catch parameter if it exists.
5426 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005427 ExcObject =
5428 CGF.Builder.CreateBitCast(ExcObject,
5429 CGF.ConvertType(CatchParam->getType()));
5430 // CatchParam is a ParmVarDecl because of the grammar
5431 // construction used to handle this, but for codegen purposes
5432 // we treat this as a local decl.
5433 CGF.EmitLocalBlockVarDecl(*CatchParam);
5434 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005435 }
5436
5437 CGF.ObjCEHValueStack.push_back(ExcObject);
5438 CGF.EmitStmt(CatchBody);
5439 CGF.ObjCEHValueStack.pop_back();
5440
5441 CGF.EmitBranchThroughCleanup(FinallyEnd);
5442
5443 CGF.EmitBlock(MatchHandler);
5444
5445 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5446 // We are required to emit this call to satisfy LLVM, even
5447 // though we don't use the result.
5448 llvm::SmallVector<llvm::Value*, 8> Args;
5449 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005450 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005451 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5452 0));
5453 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5454 CGF.Builder.CreateStore(Exc, RethrowPtr);
5455 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5456
5457 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5458
5459 CGF.EmitBlock(MatchEnd);
5460
5461 // Unfortunately, we also have to generate another EH frame here
5462 // in case this throws.
5463 llvm::BasicBlock *MatchEndHandler =
5464 CGF.createBasicBlock("match.end.handler");
5465 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5466 CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn,
5467 Cont, MatchEndHandler,
5468 Args.begin(), Args.begin());
5469
5470 CGF.EmitBlock(Cont);
5471 if (Info.SwitchBlock)
5472 CGF.EmitBlock(Info.SwitchBlock);
5473 if (Info.EndBlock)
5474 CGF.EmitBlock(Info.EndBlock);
5475
5476 CGF.EmitBlock(MatchEndHandler);
5477 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5478 // We are required to emit this call to satisfy LLVM, even
5479 // though we don't use the result.
5480 Args.clear();
5481 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005482 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005483 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5484 0));
5485 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5486 CGF.Builder.CreateStore(Exc, RethrowPtr);
5487 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5488
5489 if (Next)
5490 CGF.EmitBlock(Next);
5491 } else {
5492 assert(!Next && "catchup should be last handler.");
5493
5494 CGF.Builder.CreateStore(Exc, RethrowPtr);
5495 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5496 }
5497 }
5498
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005499 // Pop the cleanup entry, the @finally is outside this cleanup
5500 // scope.
5501 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5502 CGF.setInvokeDest(PrevLandingPad);
5503
5504 CGF.EmitBlock(FinallyBlock);
5505
5506 if (isTry) {
5507 if (const ObjCAtFinallyStmt* FinallyStmt =
5508 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5509 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5510 } else {
5511 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5512 // @synchronized.
5513 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005514 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005515
5516 if (Info.SwitchBlock)
5517 CGF.EmitBlock(Info.SwitchBlock);
5518 if (Info.EndBlock)
5519 CGF.EmitBlock(Info.EndBlock);
5520
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005521 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005522 CGF.EmitBranch(FinallyEnd);
5523
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005524 CGF.EmitBlock(FinallyRethrow);
5525 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn,
5526 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005527 CGF.Builder.CreateUnreachable();
5528
5529 CGF.EmitBlock(FinallyEnd);
5530}
5531
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005532/// EmitThrowStmt - Generate code for a throw statement.
5533void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5534 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005535 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005536 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005537 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005538 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005539 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5540 "Unexpected rethrow outside @catch block.");
5541 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005542 }
5543
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005544 llvm::Value *ExceptionAsObject =
5545 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5546 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5547 if (InvokeDest) {
5548 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5549 CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn,
5550 Cont, InvokeDest,
5551 &ExceptionAsObject, &ExceptionAsObject + 1);
5552 CGF.EmitBlock(Cont);
5553 } else
5554 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
5555 CGF.Builder.CreateUnreachable();
5556
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005557 // Clear the insertion point to indicate we are in unreachable code.
5558 CGF.Builder.ClearInsertionPoint();
5559}
Daniel Dunbare588b992009-03-01 04:46:24 +00005560
5561llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005562CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5563 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005564 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005565
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005566 // If we don't need a definition, return the entry if found or check
5567 // if we use an external reference.
5568 if (!ForDefinition) {
5569 if (Entry)
5570 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005571
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005572 // If this type (or a super class) has the __objc_exception__
5573 // attribute, emit an external reference.
5574 if (hasObjCExceptionAttribute(ID))
5575 return Entry =
5576 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5577 llvm::GlobalValue::ExternalLinkage,
5578 0,
5579 (std::string("OBJC_EHTYPE_$_") +
5580 ID->getIdentifier()->getName()),
5581 &CGM.getModule());
5582 }
5583
5584 // Otherwise we need to either make a new entry or fill in the
5585 // initializer.
5586 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005587 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005588 std::string VTableName = "objc_ehtype_vtable";
5589 llvm::GlobalVariable *VTableGV =
5590 CGM.getModule().getGlobalVariable(VTableName);
5591 if (!VTableGV)
5592 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5593 llvm::GlobalValue::ExternalLinkage,
5594 0, VTableName, &CGM.getModule());
5595
5596 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5597
5598 std::vector<llvm::Constant*> Values(3);
5599 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5600 Values[1] = GetClassName(ID->getIdentifier());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005601 Values[2] = GetClassGlobal(ClassName, false);
Daniel Dunbare588b992009-03-01 04:46:24 +00005602 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5603
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005604 if (Entry) {
5605 Entry->setInitializer(Init);
5606 } else {
5607 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5608 llvm::GlobalValue::WeakAnyLinkage,
5609 Init,
5610 (std::string("OBJC_EHTYPE_$_") +
5611 ID->getIdentifier()->getName()),
5612 &CGM.getModule());
5613 }
5614
Daniel Dunbar04d40782009-04-14 06:00:08 +00005615 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005616 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005617 Entry->setAlignment(8);
5618
5619 if (ForDefinition) {
5620 Entry->setSection("__DATA,__objc_const");
5621 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5622 } else {
5623 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5624 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005625
5626 return Entry;
5627}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005628
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005629/* *** */
5630
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005631CodeGen::CGObjCRuntime *
5632CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005633 return new CGObjCMac(CGM);
5634}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005635
5636CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005637CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005638 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005639}