CGObjCMac.cpp revision 3c267e6262bb319c8e56c9ea1283c7087f9ac107
1//===------- 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 targeting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
15
16#include "CGRecordLayout.h"
17#include "CodeGenModule.h"
18#include "CodeGenFunction.h"
19#include "CGBlocks.h"
20#include "CGCleanup.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/RecordLayout.h"
25#include "clang/AST/StmtObjC.h"
26#include "clang/Basic/LangOptions.h"
27#include "clang/Frontend/CodeGenOptions.h"
28
29#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
31#include "llvm/LLVMContext.h"
32#include "llvm/Module.h"
33#include "llvm/ADT/DenseSet.h"
34#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
36#include "llvm/ADT/SmallPtrSet.h"
37#include "llvm/Support/CallSite.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/Target/TargetData.h"
40#include <cstdio>
41
42using namespace clang;
43using namespace CodeGen;
44
45
46namespace {
47
48typedef std::vector<llvm::Constant*> ConstantVector;
49
50// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
52
53class ObjCCommonTypesHelper {
54protected:
55  llvm::LLVMContext &VMContext;
56
57private:
58  // The types of these functions don't really matter because we
59  // should always bitcast before calling them.
60
61  /// id objc_msgSend (id, SEL, ...)
62  ///
63  /// The default messenger, used for sends whose ABI is unchanged from
64  /// the all-integer/pointer case.
65  llvm::Constant *getMessageSendFn() const {
66    // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67    // be called a lot.
68    llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
69    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70                                                             params, true),
71                                     "objc_msgSend",
72                                     llvm::Attribute::NonLazyBind);
73  }
74
75  /// void objc_msgSend_stret (id, SEL, ...)
76  ///
77  /// The messenger used when the return value is an aggregate returned
78  /// by indirect reference in the first argument, and therefore the
79  /// self and selector parameters are shifted over by one.
80  llvm::Constant *getMessageSendStretFn() const {
81    llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
82    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83                                                             params, true),
84                                     "objc_msgSend_stret");
85
86  }
87
88  /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89  ///
90  /// The messenger used when the return value is returned on the x87
91  /// floating-point stack; without a special entrypoint, the nil case
92  /// would be unbalanced.
93  llvm::Constant *getMessageSendFpretFn() const {
94    llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
95    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
96                                             llvm::Type::getDoubleTy(VMContext),
97                                                        params, true),
98                                     "objc_msgSend_fpret");
99
100  }
101
102  /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
103  ///
104  /// The messenger used when the return value is returned in two values on the
105  /// x87 floating point stack; without a special entrypoint, the nil case
106  /// would be unbalanced. Only used on 64-bit X86.
107  llvm::Constant *getMessageSendFp2retFn() const {
108    llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
109    llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
110    llvm::Type *resultType =
111      llvm::StructType::get(longDoubleType, longDoubleType, NULL);
112
113    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
114                                                             params, true),
115                                     "objc_msgSend_fp2ret");
116  }
117
118  /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
119  ///
120  /// The messenger used for super calls, which have different dispatch
121  /// semantics.  The class passed is the superclass of the current
122  /// class.
123  llvm::Constant *getMessageSendSuperFn() const {
124    llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
125    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
126                                                             params, true),
127                                     "objc_msgSendSuper");
128  }
129
130  /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
131  ///
132  /// A slightly different messenger used for super calls.  The class
133  /// passed is the current class.
134  llvm::Constant *getMessageSendSuperFn2() const {
135    llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
136    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
137                                                             params, true),
138                                     "objc_msgSendSuper2");
139  }
140
141  /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
142  ///                              SEL op, ...)
143  ///
144  /// The messenger used for super calls which return an aggregate indirectly.
145  llvm::Constant *getMessageSendSuperStretFn() const {
146    llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
147    return CGM.CreateRuntimeFunction(
148      llvm::FunctionType::get(CGM.VoidTy, params, true),
149      "objc_msgSendSuper_stret");
150  }
151
152  /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
153  ///                               SEL op, ...)
154  ///
155  /// objc_msgSendSuper_stret with the super2 semantics.
156  llvm::Constant *getMessageSendSuperStretFn2() const {
157    llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
158    return CGM.CreateRuntimeFunction(
159      llvm::FunctionType::get(CGM.VoidTy, params, true),
160      "objc_msgSendSuper2_stret");
161  }
162
163  llvm::Constant *getMessageSendSuperFpretFn() const {
164    // There is no objc_msgSendSuper_fpret? How can that work?
165    return getMessageSendSuperFn();
166  }
167
168  llvm::Constant *getMessageSendSuperFpretFn2() const {
169    // There is no objc_msgSendSuper_fpret? How can that work?
170    return getMessageSendSuperFn2();
171  }
172
173protected:
174  CodeGen::CodeGenModule &CGM;
175
176public:
177  llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
178  llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
179
180  /// ObjectPtrTy - LLVM type for object handles (typeof(id))
181  llvm::Type *ObjectPtrTy;
182
183  /// PtrObjectPtrTy - LLVM type for id *
184  llvm::Type *PtrObjectPtrTy;
185
186  /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
187  llvm::Type *SelectorPtrTy;
188
189private:
190  /// ProtocolPtrTy - LLVM type for external protocol handles
191  /// (typeof(Protocol))
192  llvm::Type *ExternalProtocolPtrTy;
193
194public:
195  llvm::Type *getExternalProtocolPtrTy() {
196    if (!ExternalProtocolPtrTy) {
197      // FIXME: It would be nice to unify this with the opaque type, so that the
198      // IR comes out a bit cleaner.
199      CodeGen::CodeGenTypes &Types = CGM.getTypes();
200      ASTContext &Ctx = CGM.getContext();
201      llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
202      ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
203    }
204
205    return ExternalProtocolPtrTy;
206  }
207
208  // SuperCTy - clang type for struct objc_super.
209  QualType SuperCTy;
210  // SuperPtrCTy - clang type for struct objc_super *.
211  QualType SuperPtrCTy;
212
213  /// SuperTy - LLVM type for struct objc_super.
214  llvm::StructType *SuperTy;
215  /// SuperPtrTy - LLVM type for struct objc_super *.
216  llvm::Type *SuperPtrTy;
217
218  /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
219  /// in GCC parlance).
220  llvm::StructType *PropertyTy;
221
222  /// PropertyListTy - LLVM type for struct objc_property_list
223  /// (_prop_list_t in GCC parlance).
224  llvm::StructType *PropertyListTy;
225  /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
226  llvm::Type *PropertyListPtrTy;
227
228  // MethodTy - LLVM type for struct objc_method.
229  llvm::StructType *MethodTy;
230
231  /// CacheTy - LLVM type for struct objc_cache.
232  llvm::Type *CacheTy;
233  /// CachePtrTy - LLVM type for struct objc_cache *.
234  llvm::Type *CachePtrTy;
235
236  llvm::Constant *getGetPropertyFn() {
237    CodeGen::CodeGenTypes &Types = CGM.getTypes();
238    ASTContext &Ctx = CGM.getContext();
239    // id objc_getProperty (id, SEL, ptrdiff_t, bool)
240    SmallVector<CanQualType,4> Params;
241    CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
242    CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
243    Params.push_back(IdType);
244    Params.push_back(SelType);
245    Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
246    Params.push_back(Ctx.BoolTy);
247    llvm::FunctionType *FTy =
248      Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
249                                                  FunctionType::ExtInfo()),
250                            false);
251    return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
252  }
253
254  llvm::Constant *getSetPropertyFn() {
255    CodeGen::CodeGenTypes &Types = CGM.getTypes();
256    ASTContext &Ctx = CGM.getContext();
257    // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
258    SmallVector<CanQualType,6> Params;
259    CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
260    CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
261    Params.push_back(IdType);
262    Params.push_back(SelType);
263    Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
264    Params.push_back(IdType);
265    Params.push_back(Ctx.BoolTy);
266    Params.push_back(Ctx.BoolTy);
267    llvm::FunctionType *FTy =
268      Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
269                                                  FunctionType::ExtInfo()),
270                            false);
271    return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
272  }
273
274
275  llvm::Constant *getCopyStructFn() {
276    CodeGen::CodeGenTypes &Types = CGM.getTypes();
277    ASTContext &Ctx = CGM.getContext();
278    // void objc_copyStruct (void *, const void *, size_t, bool, bool)
279    SmallVector<CanQualType,5> Params;
280    Params.push_back(Ctx.VoidPtrTy);
281    Params.push_back(Ctx.VoidPtrTy);
282    Params.push_back(Ctx.LongTy);
283    Params.push_back(Ctx.BoolTy);
284    Params.push_back(Ctx.BoolTy);
285    llvm::FunctionType *FTy =
286      Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
287                                                  FunctionType::ExtInfo()),
288                            false);
289    return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
290  }
291
292  /// This routine declares and returns address of:
293  /// void objc_copyCppObjectAtomic(
294  ///         void *dest, const void *src,
295  ///         void (*copyHelper) (void *dest, const void *source));
296  llvm::Constant *getCppAtomicObjectFunction() {
297    CodeGen::CodeGenTypes &Types = CGM.getTypes();
298    ASTContext &Ctx = CGM.getContext();
299    /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
300    SmallVector<CanQualType,3> Params;
301    Params.push_back(Ctx.VoidPtrTy);
302    Params.push_back(Ctx.VoidPtrTy);
303    Params.push_back(Ctx.VoidPtrTy);
304    llvm::FunctionType *FTy =
305    Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
306                                                FunctionType::ExtInfo()),
307                          false);
308    return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
309  }
310
311  llvm::Constant *getEnumerationMutationFn() {
312    CodeGen::CodeGenTypes &Types = CGM.getTypes();
313    ASTContext &Ctx = CGM.getContext();
314    // void objc_enumerationMutation (id)
315    SmallVector<CanQualType,1> Params;
316    Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
317    llvm::FunctionType *FTy =
318      Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
319                                                  FunctionType::ExtInfo()),
320                            false);
321    return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
322  }
323
324  /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
325  llvm::Constant *getGcReadWeakFn() {
326    // id objc_read_weak (id *)
327    llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
328    llvm::FunctionType *FTy =
329      llvm::FunctionType::get(ObjectPtrTy, args, false);
330    return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
331  }
332
333  /// GcAssignWeakFn -- LLVM objc_assign_weak function.
334  llvm::Constant *getGcAssignWeakFn() {
335    // id objc_assign_weak (id, id *)
336    llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
337    llvm::FunctionType *FTy =
338      llvm::FunctionType::get(ObjectPtrTy, args, false);
339    return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
340  }
341
342  /// GcAssignGlobalFn -- LLVM objc_assign_global function.
343  llvm::Constant *getGcAssignGlobalFn() {
344    // id objc_assign_global(id, id *)
345    llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
346    llvm::FunctionType *FTy =
347      llvm::FunctionType::get(ObjectPtrTy, args, false);
348    return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
349  }
350
351  /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
352  llvm::Constant *getGcAssignThreadLocalFn() {
353    // id objc_assign_threadlocal(id src, id * dest)
354    llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
355    llvm::FunctionType *FTy =
356      llvm::FunctionType::get(ObjectPtrTy, args, false);
357    return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
358  }
359
360  /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
361  llvm::Constant *getGcAssignIvarFn() {
362    // id objc_assign_ivar(id, id *, ptrdiff_t)
363    llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
364                           CGM.PtrDiffTy };
365    llvm::FunctionType *FTy =
366      llvm::FunctionType::get(ObjectPtrTy, args, false);
367    return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
368  }
369
370  /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
371  llvm::Constant *GcMemmoveCollectableFn() {
372    // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
373    llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
374    llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
375    return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
376  }
377
378  /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
379  llvm::Constant *getGcAssignStrongCastFn() {
380    // id objc_assign_strongCast(id, id *)
381    llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
382    llvm::FunctionType *FTy =
383      llvm::FunctionType::get(ObjectPtrTy, args, false);
384    return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
385  }
386
387  /// ExceptionThrowFn - LLVM objc_exception_throw function.
388  llvm::Constant *getExceptionThrowFn() {
389    // void objc_exception_throw(id)
390    llvm::Type *args[] = { ObjectPtrTy };
391    llvm::FunctionType *FTy =
392      llvm::FunctionType::get(CGM.VoidTy, args, false);
393    return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
394  }
395
396  /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
397  llvm::Constant *getExceptionRethrowFn() {
398    // void objc_exception_rethrow(void)
399    llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
400    return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
401  }
402
403  /// SyncEnterFn - LLVM object_sync_enter function.
404  llvm::Constant *getSyncEnterFn() {
405    // void objc_sync_enter (id)
406    llvm::Type *args[] = { ObjectPtrTy };
407    llvm::FunctionType *FTy =
408      llvm::FunctionType::get(CGM.VoidTy, args, false);
409    return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
410  }
411
412  /// SyncExitFn - LLVM object_sync_exit function.
413  llvm::Constant *getSyncExitFn() {
414    // void objc_sync_exit (id)
415    llvm::Type *args[] = { ObjectPtrTy };
416    llvm::FunctionType *FTy =
417      llvm::FunctionType::get(CGM.VoidTy, args, false);
418    return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
419  }
420
421  llvm::Constant *getSendFn(bool IsSuper) const {
422    return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
423  }
424
425  llvm::Constant *getSendFn2(bool IsSuper) const {
426    return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
427  }
428
429  llvm::Constant *getSendStretFn(bool IsSuper) const {
430    return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
431  }
432
433  llvm::Constant *getSendStretFn2(bool IsSuper) const {
434    return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
435  }
436
437  llvm::Constant *getSendFpretFn(bool IsSuper) const {
438    return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
439  }
440
441  llvm::Constant *getSendFpretFn2(bool IsSuper) const {
442    return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
443  }
444
445  llvm::Constant *getSendFp2retFn(bool IsSuper) const {
446    return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
447  }
448
449  llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
450    return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
451  }
452
453  ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
454  ~ObjCCommonTypesHelper(){}
455};
456
457/// ObjCTypesHelper - Helper class that encapsulates lazy
458/// construction of varies types used during ObjC generation.
459class ObjCTypesHelper : public ObjCCommonTypesHelper {
460public:
461  /// SymtabTy - LLVM type for struct objc_symtab.
462  llvm::StructType *SymtabTy;
463  /// SymtabPtrTy - LLVM type for struct objc_symtab *.
464  llvm::Type *SymtabPtrTy;
465  /// ModuleTy - LLVM type for struct objc_module.
466  llvm::StructType *ModuleTy;
467
468  /// ProtocolTy - LLVM type for struct objc_protocol.
469  llvm::StructType *ProtocolTy;
470  /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
471  llvm::Type *ProtocolPtrTy;
472  /// ProtocolExtensionTy - LLVM type for struct
473  /// objc_protocol_extension.
474  llvm::StructType *ProtocolExtensionTy;
475  /// ProtocolExtensionTy - LLVM type for struct
476  /// objc_protocol_extension *.
477  llvm::Type *ProtocolExtensionPtrTy;
478  /// MethodDescriptionTy - LLVM type for struct
479  /// objc_method_description.
480  llvm::StructType *MethodDescriptionTy;
481  /// MethodDescriptionListTy - LLVM type for struct
482  /// objc_method_description_list.
483  llvm::StructType *MethodDescriptionListTy;
484  /// MethodDescriptionListPtrTy - LLVM type for struct
485  /// objc_method_description_list *.
486  llvm::Type *MethodDescriptionListPtrTy;
487  /// ProtocolListTy - LLVM type for struct objc_property_list.
488  llvm::StructType *ProtocolListTy;
489  /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
490  llvm::Type *ProtocolListPtrTy;
491  /// CategoryTy - LLVM type for struct objc_category.
492  llvm::StructType *CategoryTy;
493  /// ClassTy - LLVM type for struct objc_class.
494  llvm::StructType *ClassTy;
495  /// ClassPtrTy - LLVM type for struct objc_class *.
496  llvm::Type *ClassPtrTy;
497  /// ClassExtensionTy - LLVM type for struct objc_class_ext.
498  llvm::StructType *ClassExtensionTy;
499  /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
500  llvm::Type *ClassExtensionPtrTy;
501  // IvarTy - LLVM type for struct objc_ivar.
502  llvm::StructType *IvarTy;
503  /// IvarListTy - LLVM type for struct objc_ivar_list.
504  llvm::Type *IvarListTy;
505  /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
506  llvm::Type *IvarListPtrTy;
507  /// MethodListTy - LLVM type for struct objc_method_list.
508  llvm::Type *MethodListTy;
509  /// MethodListPtrTy - LLVM type for struct objc_method_list *.
510  llvm::Type *MethodListPtrTy;
511
512  /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
513  llvm::Type *ExceptionDataTy;
514
515  /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
516  llvm::Constant *getExceptionTryEnterFn() {
517    llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
518    return CGM.CreateRuntimeFunction(
519      llvm::FunctionType::get(CGM.VoidTy, params, false),
520      "objc_exception_try_enter");
521  }
522
523  /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
524  llvm::Constant *getExceptionTryExitFn() {
525    llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
526    return CGM.CreateRuntimeFunction(
527      llvm::FunctionType::get(CGM.VoidTy, params, false),
528      "objc_exception_try_exit");
529  }
530
531  /// ExceptionExtractFn - LLVM objc_exception_extract function.
532  llvm::Constant *getExceptionExtractFn() {
533    llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
534    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
535                                                             params, false),
536                                     "objc_exception_extract");
537  }
538
539  /// ExceptionMatchFn - LLVM objc_exception_match function.
540  llvm::Constant *getExceptionMatchFn() {
541    llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
542    return CGM.CreateRuntimeFunction(
543      llvm::FunctionType::get(CGM.Int32Ty, params, false),
544      "objc_exception_match");
545
546  }
547
548  /// SetJmpFn - LLVM _setjmp function.
549  llvm::Constant *getSetJmpFn() {
550    // This is specifically the prototype for x86.
551    llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
552    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
553                                                             params, false),
554                                     "_setjmp",
555                                     llvm::Attribute::ReturnsTwice);
556  }
557
558public:
559  ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
560  ~ObjCTypesHelper() {}
561};
562
563/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
564/// modern abi
565class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
566public:
567
568  // MethodListnfABITy - LLVM for struct _method_list_t
569  llvm::StructType *MethodListnfABITy;
570
571  // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
572  llvm::Type *MethodListnfABIPtrTy;
573
574  // ProtocolnfABITy = LLVM for struct _protocol_t
575  llvm::StructType *ProtocolnfABITy;
576
577  // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
578  llvm::Type *ProtocolnfABIPtrTy;
579
580  // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
581  llvm::StructType *ProtocolListnfABITy;
582
583  // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
584  llvm::Type *ProtocolListnfABIPtrTy;
585
586  // ClassnfABITy - LLVM for struct _class_t
587  llvm::StructType *ClassnfABITy;
588
589  // ClassnfABIPtrTy - LLVM for struct _class_t*
590  llvm::Type *ClassnfABIPtrTy;
591
592  // IvarnfABITy - LLVM for struct _ivar_t
593  llvm::StructType *IvarnfABITy;
594
595  // IvarListnfABITy - LLVM for struct _ivar_list_t
596  llvm::StructType *IvarListnfABITy;
597
598  // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
599  llvm::Type *IvarListnfABIPtrTy;
600
601  // ClassRonfABITy - LLVM for struct _class_ro_t
602  llvm::StructType *ClassRonfABITy;
603
604  // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
605  llvm::Type *ImpnfABITy;
606
607  // CategorynfABITy - LLVM for struct _category_t
608  llvm::StructType *CategorynfABITy;
609
610  // New types for nonfragile abi messaging.
611
612  // MessageRefTy - LLVM for:
613  // struct _message_ref_t {
614  //   IMP messenger;
615  //   SEL name;
616  // };
617  llvm::StructType *MessageRefTy;
618  // MessageRefCTy - clang type for struct _message_ref_t
619  QualType MessageRefCTy;
620
621  // MessageRefPtrTy - LLVM for struct _message_ref_t*
622  llvm::Type *MessageRefPtrTy;
623  // MessageRefCPtrTy - clang type for struct _message_ref_t*
624  QualType MessageRefCPtrTy;
625
626  // MessengerTy - Type of the messenger (shown as IMP above)
627  llvm::FunctionType *MessengerTy;
628
629  // SuperMessageRefTy - LLVM for:
630  // struct _super_message_ref_t {
631  //   SUPER_IMP messenger;
632  //   SEL name;
633  // };
634  llvm::StructType *SuperMessageRefTy;
635
636  // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
637  llvm::Type *SuperMessageRefPtrTy;
638
639  llvm::Constant *getMessageSendFixupFn() {
640    // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
641    llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
642    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
643                                                             params, true),
644                                     "objc_msgSend_fixup");
645  }
646
647  llvm::Constant *getMessageSendFpretFixupFn() {
648    // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
649    llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
650    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
651                                                             params, true),
652                                     "objc_msgSend_fpret_fixup");
653  }
654
655  llvm::Constant *getMessageSendStretFixupFn() {
656    // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
657    llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
658    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
659                                                             params, true),
660                                     "objc_msgSend_stret_fixup");
661  }
662
663  llvm::Constant *getMessageSendSuper2FixupFn() {
664    // id objc_msgSendSuper2_fixup (struct objc_super *,
665    //                              struct _super_message_ref_t*, ...)
666    llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
667    return  CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
668                                                              params, true),
669                                      "objc_msgSendSuper2_fixup");
670  }
671
672  llvm::Constant *getMessageSendSuper2StretFixupFn() {
673    // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
674    //                                   struct _super_message_ref_t*, ...)
675    llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
676    return  CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
677                                                              params, true),
678                                      "objc_msgSendSuper2_stret_fixup");
679  }
680
681  llvm::Constant *getObjCEndCatchFn() {
682    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
683                                     "objc_end_catch");
684
685  }
686
687  llvm::Constant *getObjCBeginCatchFn() {
688    llvm::Type *params[] = { Int8PtrTy };
689    return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
690                                                             params, false),
691                                     "objc_begin_catch");
692  }
693
694  llvm::StructType *EHTypeTy;
695  llvm::Type *EHTypePtrTy;
696
697  ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
698  ~ObjCNonFragileABITypesHelper(){}
699};
700
701class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
702public:
703  // FIXME - accessibility
704  class GC_IVAR {
705  public:
706    unsigned ivar_bytepos;
707    unsigned ivar_size;
708    GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
709      : ivar_bytepos(bytepos), ivar_size(size) {}
710
711    // Allow sorting based on byte pos.
712    bool operator<(const GC_IVAR &b) const {
713      return ivar_bytepos < b.ivar_bytepos;
714    }
715  };
716
717  class SKIP_SCAN {
718  public:
719    unsigned skip;
720    unsigned scan;
721    SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
722      : skip(_skip), scan(_scan) {}
723  };
724
725protected:
726  CodeGen::CodeGenModule &CGM;
727  llvm::LLVMContext &VMContext;
728  // FIXME! May not be needing this after all.
729  unsigned ObjCABI;
730
731  // gc ivar layout bitmap calculation helper caches.
732  SmallVector<GC_IVAR, 16> SkipIvars;
733  SmallVector<GC_IVAR, 16> IvarsInfo;
734
735  /// LazySymbols - Symbols to generate a lazy reference for. See
736  /// DefinedSymbols and FinishModule().
737  llvm::SetVector<IdentifierInfo*> LazySymbols;
738
739  /// DefinedSymbols - External symbols which are defined by this
740  /// module. The symbols in this list and LazySymbols are used to add
741  /// special linker symbols which ensure that Objective-C modules are
742  /// linked properly.
743  llvm::SetVector<IdentifierInfo*> DefinedSymbols;
744
745  /// ClassNames - uniqued class names.
746  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
747
748  /// MethodVarNames - uniqued method variable names.
749  llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
750
751  /// DefinedCategoryNames - list of category names in form Class_Category.
752  llvm::SetVector<std::string> DefinedCategoryNames;
753
754  /// MethodVarTypes - uniqued method type signatures. We have to use
755  /// a StringMap here because have no other unique reference.
756  llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
757
758  /// MethodDefinitions - map of methods which have been defined in
759  /// this translation unit.
760  llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
761
762  /// PropertyNames - uniqued method variable names.
763  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
764
765  /// ClassReferences - uniqued class references.
766  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
767
768  /// SelectorReferences - uniqued selector references.
769  llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
770
771  /// Protocols - Protocols for which an objc_protocol structure has
772  /// been emitted. Forward declarations are handled by creating an
773  /// empty structure whose initializer is filled in when/if defined.
774  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
775
776  /// DefinedProtocols - Protocols which have actually been
777  /// defined. We should not need this, see FIXME in GenerateProtocol.
778  llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
779
780  /// DefinedClasses - List of defined classes.
781  std::vector<llvm::GlobalValue*> DefinedClasses;
782
783  /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
784  std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
785
786  /// DefinedCategories - List of defined categories.
787  std::vector<llvm::GlobalValue*> DefinedCategories;
788
789  /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
790  std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
791
792  /// GetNameForMethod - Return a name for the given method.
793  /// \param[out] NameOut - The return value.
794  void GetNameForMethod(const ObjCMethodDecl *OMD,
795                        const ObjCContainerDecl *CD,
796                        SmallVectorImpl<char> &NameOut);
797
798  /// GetMethodVarName - Return a unique constant for the given
799  /// selector's name. The return value has type char *.
800  llvm::Constant *GetMethodVarName(Selector Sel);
801  llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
802
803  /// GetMethodVarType - Return a unique constant for the given
804  /// method's type encoding string. The return value has type char *.
805
806  // FIXME: This is a horrible name.
807  llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
808                                   bool Extended = false);
809  llvm::Constant *GetMethodVarType(const FieldDecl *D);
810
811  /// GetPropertyName - Return a unique constant for the given
812  /// name. The return value has type char *.
813  llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
814
815  // FIXME: This can be dropped once string functions are unified.
816  llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
817                                        const Decl *Container);
818
819  /// GetClassName - Return a unique constant for the given selector's
820  /// name. The return value has type char *.
821  llvm::Constant *GetClassName(IdentifierInfo *Ident);
822
823  llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
824
825  /// BuildIvarLayout - Builds ivar layout bitmap for the class
826  /// implementation for the __strong or __weak case.
827  ///
828  llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
829                                  bool ForStrongLayout);
830
831  llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
832
833  void BuildAggrIvarRecordLayout(const RecordType *RT,
834                                 unsigned int BytePos, bool ForStrongLayout,
835                                 bool &HasUnion);
836  void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
837                           const llvm::StructLayout *Layout,
838                           const RecordDecl *RD,
839                           const SmallVectorImpl<const FieldDecl*> &RecFields,
840                           unsigned int BytePos, bool ForStrongLayout,
841                           bool &HasUnion);
842
843  /// GetIvarLayoutName - Returns a unique constant for the given
844  /// ivar layout bitmap.
845  llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
846                                    const ObjCCommonTypesHelper &ObjCTypes);
847
848  /// EmitPropertyList - Emit the given property list. The return
849  /// value has type PropertyListPtrTy.
850  llvm::Constant *EmitPropertyList(Twine Name,
851                                   const Decl *Container,
852                                   const ObjCContainerDecl *OCD,
853                                   const ObjCCommonTypesHelper &ObjCTypes);
854
855  /// EmitProtocolMethodTypes - Generate the array of extended method type
856  /// strings. The return value has type Int8PtrPtrTy.
857  llvm::Constant *EmitProtocolMethodTypes(Twine Name,
858                                          const ConstantVector &MethodTypes,
859                                       const ObjCCommonTypesHelper &ObjCTypes);
860
861  /// PushProtocolProperties - Push protocol's property on the input stack.
862  void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
863                              std::vector<llvm::Constant*> &Properties,
864                                   const Decl *Container,
865                                   const ObjCProtocolDecl *PROTO,
866                                   const ObjCCommonTypesHelper &ObjCTypes);
867
868  /// GetProtocolRef - Return a reference to the internal protocol
869  /// description, creating an empty one if it has not been
870  /// defined. The return value has type ProtocolPtrTy.
871  llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
872
873  /// CreateMetadataVar - Create a global variable with internal
874  /// linkage for use by the Objective-C runtime.
875  ///
876  /// This is a convenience wrapper which not only creates the
877  /// variable, but also sets the section and alignment and adds the
878  /// global to the "llvm.used" list.
879  ///
880  /// \param Name - The variable name.
881  /// \param Init - The variable initializer; this is also used to
882  /// define the type of the variable.
883  /// \param Section - The section the variable should go into, or 0.
884  /// \param Align - The alignment for the variable, or 0.
885  /// \param AddToUsed - Whether the variable should be added to
886  /// "llvm.used".
887  llvm::GlobalVariable *CreateMetadataVar(Twine Name,
888                                          llvm::Constant *Init,
889                                          const char *Section,
890                                          unsigned Align,
891                                          bool AddToUsed);
892
893  CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
894                                  ReturnValueSlot Return,
895                                  QualType ResultType,
896                                  llvm::Value *Sel,
897                                  llvm::Value *Arg0,
898                                  QualType Arg0Ty,
899                                  bool IsSuper,
900                                  const CallArgList &CallArgs,
901                                  const ObjCMethodDecl *OMD,
902                                  const ObjCCommonTypesHelper &ObjCTypes);
903
904  /// EmitImageInfo - Emit the image info marker used to encode some module
905  /// level information.
906  void EmitImageInfo();
907
908public:
909  CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
910    CGM(cgm), VMContext(cgm.getLLVMContext()) { }
911
912  virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
913
914  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
915                                         const ObjCContainerDecl *CD=0);
916
917  virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
918
919  /// GetOrEmitProtocol - Get the protocol object for the given
920  /// declaration, emitting it if necessary. The return value has type
921  /// ProtocolPtrTy.
922  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
923
924  /// GetOrEmitProtocolRef - Get a forward reference to the protocol
925  /// object for the given declaration, emitting it if needed. These
926  /// forward references will be filled in with empty bodies if no
927  /// definition is seen. The return value has type ProtocolPtrTy.
928  virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
929  virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
930                                             const CGBlockInfo &blockInfo);
931
932};
933
934class CGObjCMac : public CGObjCCommonMac {
935private:
936  ObjCTypesHelper ObjCTypes;
937
938  /// EmitModuleInfo - Another marker encoding module level
939  /// information.
940  void EmitModuleInfo();
941
942  /// EmitModuleSymols - Emit module symbols, the list of defined
943  /// classes and categories. The result has type SymtabPtrTy.
944  llvm::Constant *EmitModuleSymbols();
945
946  /// FinishModule - Write out global data structures at the end of
947  /// processing a translation unit.
948  void FinishModule();
949
950  /// EmitClassExtension - Generate the class extension structure used
951  /// to store the weak ivar layout and properties. The return value
952  /// has type ClassExtensionPtrTy.
953  llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
954
955  /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
956  /// for the given class.
957  llvm::Value *EmitClassRef(CGBuilderTy &Builder,
958                            const ObjCInterfaceDecl *ID);
959
960  llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
961                                  IdentifierInfo *II);
962
963  llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
964
965  /// EmitSuperClassRef - Emits reference to class's main metadata class.
966  llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
967
968  /// EmitIvarList - Emit the ivar list for the given
969  /// implementation. If ForClass is true the list of class ivars
970  /// (i.e. metaclass ivars) is emitted, otherwise the list of
971  /// interface ivars will be emitted. The return value has type
972  /// IvarListPtrTy.
973  llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
974                               bool ForClass);
975
976  /// EmitMetaClass - Emit a forward reference to the class structure
977  /// for the metaclass of the given interface. The return value has
978  /// type ClassPtrTy.
979  llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
980
981  /// EmitMetaClass - Emit a class structure for the metaclass of the
982  /// given implementation. The return value has type ClassPtrTy.
983  llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
984                                llvm::Constant *Protocols,
985                                const ConstantVector &Methods);
986
987  llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
988
989  llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
990
991  /// EmitMethodList - Emit the method list for the given
992  /// implementation. The return value has type MethodListPtrTy.
993  llvm::Constant *EmitMethodList(Twine Name,
994                                 const char *Section,
995                                 const ConstantVector &Methods);
996
997  /// EmitMethodDescList - Emit a method description list for a list of
998  /// method declarations.
999  ///  - TypeName: The name for the type containing the methods.
1000  ///  - IsProtocol: True iff these methods are for a protocol.
1001  ///  - ClassMethds: True iff these are class methods.
1002  ///  - Required: When true, only "required" methods are
1003  ///    listed. Similarly, when false only "optional" methods are
1004  ///    listed. For classes this should always be true.
1005  ///  - begin, end: The method list to output.
1006  ///
1007  /// The return value has type MethodDescriptionListPtrTy.
1008  llvm::Constant *EmitMethodDescList(Twine Name,
1009                                     const char *Section,
1010                                     const ConstantVector &Methods);
1011
1012  /// GetOrEmitProtocol - Get the protocol object for the given
1013  /// declaration, emitting it if necessary. The return value has type
1014  /// ProtocolPtrTy.
1015  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1016
1017  /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1018  /// object for the given declaration, emitting it if needed. These
1019  /// forward references will be filled in with empty bodies if no
1020  /// definition is seen. The return value has type ProtocolPtrTy.
1021  virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1022
1023  /// EmitProtocolExtension - Generate the protocol extension
1024  /// structure used to store optional instance and class methods, and
1025  /// protocol properties. The return value has type
1026  /// ProtocolExtensionPtrTy.
1027  llvm::Constant *
1028  EmitProtocolExtension(const ObjCProtocolDecl *PD,
1029                        const ConstantVector &OptInstanceMethods,
1030                        const ConstantVector &OptClassMethods,
1031                        const ConstantVector &MethodTypesExt);
1032
1033  /// EmitProtocolList - Generate the list of referenced
1034  /// protocols. The return value has type ProtocolListPtrTy.
1035  llvm::Constant *EmitProtocolList(Twine Name,
1036                                   ObjCProtocolDecl::protocol_iterator begin,
1037                                   ObjCProtocolDecl::protocol_iterator end);
1038
1039  /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1040  /// for the given selector.
1041  llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1042                            bool lval=false);
1043
1044public:
1045  CGObjCMac(CodeGen::CodeGenModule &cgm);
1046
1047  virtual llvm::Function *ModuleInitFunction();
1048
1049  virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1050                                              ReturnValueSlot Return,
1051                                              QualType ResultType,
1052                                              Selector Sel,
1053                                              llvm::Value *Receiver,
1054                                              const CallArgList &CallArgs,
1055                                              const ObjCInterfaceDecl *Class,
1056                                              const ObjCMethodDecl *Method);
1057
1058  virtual CodeGen::RValue
1059  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1060                           ReturnValueSlot Return,
1061                           QualType ResultType,
1062                           Selector Sel,
1063                           const ObjCInterfaceDecl *Class,
1064                           bool isCategoryImpl,
1065                           llvm::Value *Receiver,
1066                           bool IsClassMessage,
1067                           const CallArgList &CallArgs,
1068                           const ObjCMethodDecl *Method);
1069
1070  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
1071                                const ObjCInterfaceDecl *ID);
1072
1073  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1074                                   bool lval = false);
1075
1076  /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1077  /// untyped one.
1078  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1079                                   const ObjCMethodDecl *Method);
1080
1081  virtual llvm::Constant *GetEHType(QualType T);
1082
1083  virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
1084
1085  virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
1086
1087  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
1088                                           const ObjCProtocolDecl *PD);
1089
1090  virtual llvm::Constant *GetPropertyGetFunction();
1091  virtual llvm::Constant *GetPropertySetFunction();
1092  virtual llvm::Constant *GetGetStructFunction();
1093  virtual llvm::Constant *GetSetStructFunction();
1094  virtual llvm::Constant *GetCppAtomicObjectFunction();
1095  virtual llvm::Constant *EnumerationMutationFunction();
1096
1097  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1098                           const ObjCAtTryStmt &S);
1099  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1100                                    const ObjCAtSynchronizedStmt &S);
1101  void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
1102  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1103                             const ObjCAtThrowStmt &S);
1104  virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
1105                                         llvm::Value *AddrWeakObj);
1106  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1107                                  llvm::Value *src, llvm::Value *dst);
1108  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1109                                    llvm::Value *src, llvm::Value *dest,
1110                                    bool threadlocal = false);
1111  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1112                                  llvm::Value *src, llvm::Value *dest,
1113                                  llvm::Value *ivarOffset);
1114  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1115                                        llvm::Value *src, llvm::Value *dest);
1116  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1117                                        llvm::Value *dest, llvm::Value *src,
1118                                        llvm::Value *size);
1119
1120  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1121                                      QualType ObjectTy,
1122                                      llvm::Value *BaseValue,
1123                                      const ObjCIvarDecl *Ivar,
1124                                      unsigned CVRQualifiers);
1125  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1126                                      const ObjCInterfaceDecl *Interface,
1127                                      const ObjCIvarDecl *Ivar);
1128
1129  /// GetClassGlobal - Return the global variable for the Objective-C
1130  /// class of the given name.
1131  virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
1132    llvm_unreachable("CGObjCMac::GetClassGlobal");
1133  }
1134};
1135
1136class CGObjCNonFragileABIMac : public CGObjCCommonMac {
1137private:
1138  ObjCNonFragileABITypesHelper ObjCTypes;
1139  llvm::GlobalVariable* ObjCEmptyCacheVar;
1140  llvm::GlobalVariable* ObjCEmptyVtableVar;
1141
1142  /// SuperClassReferences - uniqued super class references.
1143  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1144
1145  /// MetaClassReferences - uniqued meta class references.
1146  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
1147
1148  /// EHTypeReferences - uniqued class ehtype references.
1149  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
1150
1151  /// VTableDispatchMethods - List of methods for which we generate
1152  /// vtable-based message dispatch.
1153  llvm::DenseSet<Selector> VTableDispatchMethods;
1154
1155  /// DefinedMetaClasses - List of defined meta-classes.
1156  std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1157
1158  /// isVTableDispatchedSelector - Returns true if SEL is a
1159  /// vtable-based selector.
1160  bool isVTableDispatchedSelector(Selector Sel);
1161
1162  /// FinishNonFragileABIModule - Write out global data structures at the end of
1163  /// processing a translation unit.
1164  void FinishNonFragileABIModule();
1165
1166  /// AddModuleClassList - Add the given list of class pointers to the
1167  /// module with the provided symbol and section names.
1168  void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1169                          const char *SymbolName,
1170                          const char *SectionName);
1171
1172  llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1173                                              unsigned InstanceStart,
1174                                              unsigned InstanceSize,
1175                                              const ObjCImplementationDecl *ID);
1176  llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1177                                            llvm::Constant *IsAGV,
1178                                            llvm::Constant *SuperClassGV,
1179                                            llvm::Constant *ClassRoGV,
1180                                            bool HiddenVisibility);
1181
1182  llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1183
1184  llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1185
1186  /// EmitMethodList - Emit the method list for the given
1187  /// implementation. The return value has type MethodListnfABITy.
1188  llvm::Constant *EmitMethodList(Twine Name,
1189                                 const char *Section,
1190                                 const ConstantVector &Methods);
1191  /// EmitIvarList - Emit the ivar list for the given
1192  /// implementation. If ForClass is true the list of class ivars
1193  /// (i.e. metaclass ivars) is emitted, otherwise the list of
1194  /// interface ivars will be emitted. The return value has type
1195  /// IvarListnfABIPtrTy.
1196  llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
1197
1198  llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
1199                                    const ObjCIvarDecl *Ivar,
1200                                    unsigned long int offset);
1201
1202  /// GetOrEmitProtocol - Get the protocol object for the given
1203  /// declaration, emitting it if necessary. The return value has type
1204  /// ProtocolPtrTy.
1205  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1206
1207  /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1208  /// object for the given declaration, emitting it if needed. These
1209  /// forward references will be filled in with empty bodies if no
1210  /// definition is seen. The return value has type ProtocolPtrTy.
1211  virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1212
1213  /// EmitProtocolList - Generate the list of referenced
1214  /// protocols. The return value has type ProtocolListPtrTy.
1215  llvm::Constant *EmitProtocolList(Twine Name,
1216                                   ObjCProtocolDecl::protocol_iterator begin,
1217                                   ObjCProtocolDecl::protocol_iterator end);
1218
1219  CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1220                                        ReturnValueSlot Return,
1221                                        QualType ResultType,
1222                                        Selector Sel,
1223                                        llvm::Value *Receiver,
1224                                        QualType Arg0Ty,
1225                                        bool IsSuper,
1226                                        const CallArgList &CallArgs,
1227                                        const ObjCMethodDecl *Method);
1228
1229  /// GetClassGlobal - Return the global variable for the Objective-C
1230  /// class of the given name.
1231  llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1232
1233  /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1234  /// for the given class reference.
1235  llvm::Value *EmitClassRef(CGBuilderTy &Builder,
1236                            const ObjCInterfaceDecl *ID);
1237
1238  llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1239                                  IdentifierInfo *II);
1240
1241  llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1242
1243  /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1244  /// for the given super class reference.
1245  llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1246                                 const ObjCInterfaceDecl *ID);
1247
1248  /// EmitMetaClassRef - Return a Value * of the address of _class_t
1249  /// meta-data
1250  llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1251                                const ObjCInterfaceDecl *ID);
1252
1253  /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1254  /// the given ivar.
1255  ///
1256  llvm::GlobalVariable * ObjCIvarOffsetVariable(
1257    const ObjCInterfaceDecl *ID,
1258    const ObjCIvarDecl *Ivar);
1259
1260  /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1261  /// for the given selector.
1262  llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1263                            bool lval=false);
1264
1265  /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
1266  /// interface. The return value has type EHTypePtrTy.
1267  llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1268                                  bool ForDefinition);
1269
1270  const char *getMetaclassSymbolPrefix() const {
1271    return "OBJC_METACLASS_$_";
1272  }
1273
1274  const char *getClassSymbolPrefix() const {
1275    return "OBJC_CLASS_$_";
1276  }
1277
1278  void GetClassSizeInfo(const ObjCImplementationDecl *OID,
1279                        uint32_t &InstanceStart,
1280                        uint32_t &InstanceSize);
1281
1282  // Shamelessly stolen from Analysis/CFRefCount.cpp
1283  Selector GetNullarySelector(const char* name) const {
1284    IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1285    return CGM.getContext().Selectors.getSelector(0, &II);
1286  }
1287
1288  Selector GetUnarySelector(const char* name) const {
1289    IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1290    return CGM.getContext().Selectors.getSelector(1, &II);
1291  }
1292
1293  /// ImplementationIsNonLazy - Check whether the given category or
1294  /// class implementation is "non-lazy".
1295  bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
1296
1297public:
1298  CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
1299  // FIXME. All stubs for now!
1300  virtual llvm::Function *ModuleInitFunction();
1301
1302  virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1303                                              ReturnValueSlot Return,
1304                                              QualType ResultType,
1305                                              Selector Sel,
1306                                              llvm::Value *Receiver,
1307                                              const CallArgList &CallArgs,
1308                                              const ObjCInterfaceDecl *Class,
1309                                              const ObjCMethodDecl *Method);
1310
1311  virtual CodeGen::RValue
1312  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1313                           ReturnValueSlot Return,
1314                           QualType ResultType,
1315                           Selector Sel,
1316                           const ObjCInterfaceDecl *Class,
1317                           bool isCategoryImpl,
1318                           llvm::Value *Receiver,
1319                           bool IsClassMessage,
1320                           const CallArgList &CallArgs,
1321                           const ObjCMethodDecl *Method);
1322
1323  virtual llvm::Value *GetClass(CGBuilderTy &Builder,
1324                                const ObjCInterfaceDecl *ID);
1325
1326  virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1327                                   bool lvalue = false)
1328    { return EmitSelector(Builder, Sel, lvalue); }
1329
1330  /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1331  /// untyped one.
1332  virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1333                                   const ObjCMethodDecl *Method)
1334    { return EmitSelector(Builder, Method->getSelector()); }
1335
1336  virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
1337
1338  virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
1339  virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
1340                                           const ObjCProtocolDecl *PD);
1341
1342  virtual llvm::Constant *GetEHType(QualType T);
1343
1344  virtual llvm::Constant *GetPropertyGetFunction() {
1345    return ObjCTypes.getGetPropertyFn();
1346  }
1347  virtual llvm::Constant *GetPropertySetFunction() {
1348    return ObjCTypes.getSetPropertyFn();
1349  }
1350
1351  virtual llvm::Constant *GetSetStructFunction() {
1352    return ObjCTypes.getCopyStructFn();
1353  }
1354  virtual llvm::Constant *GetGetStructFunction() {
1355    return ObjCTypes.getCopyStructFn();
1356  }
1357  virtual llvm::Constant *GetCppAtomicObjectFunction() {
1358    return ObjCTypes.getCppAtomicObjectFunction();
1359  }
1360
1361  virtual llvm::Constant *EnumerationMutationFunction() {
1362    return ObjCTypes.getEnumerationMutationFn();
1363  }
1364
1365  virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1366                           const ObjCAtTryStmt &S);
1367  virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1368                                    const ObjCAtSynchronizedStmt &S);
1369  virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1370                             const ObjCAtThrowStmt &S);
1371  virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
1372                                         llvm::Value *AddrWeakObj);
1373  virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1374                                  llvm::Value *src, llvm::Value *dst);
1375  virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1376                                    llvm::Value *src, llvm::Value *dest,
1377                                    bool threadlocal = false);
1378  virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1379                                  llvm::Value *src, llvm::Value *dest,
1380                                  llvm::Value *ivarOffset);
1381  virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1382                                        llvm::Value *src, llvm::Value *dest);
1383  virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1384                                        llvm::Value *dest, llvm::Value *src,
1385                                        llvm::Value *size);
1386  virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1387                                      QualType ObjectTy,
1388                                      llvm::Value *BaseValue,
1389                                      const ObjCIvarDecl *Ivar,
1390                                      unsigned CVRQualifiers);
1391  virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1392                                      const ObjCInterfaceDecl *Interface,
1393                                      const ObjCIvarDecl *Ivar);
1394};
1395
1396/// A helper class for performing the null-initialization of a return
1397/// value.
1398struct NullReturnState {
1399  llvm::BasicBlock *NullBB;
1400  llvm::BasicBlock *callBB;
1401  NullReturnState() : NullBB(0), callBB(0) {}
1402
1403  void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1404    // Make blocks for the null-init and call edges.
1405    NullBB = CGF.createBasicBlock("msgSend.nullinit");
1406    callBB = CGF.createBasicBlock("msgSend.call");
1407
1408    // Check for a null receiver and, if there is one, jump to the
1409    // null-init test.
1410    llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1411    CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1412
1413    // Otherwise, start performing the call.
1414    CGF.EmitBlock(callBB);
1415  }
1416
1417  RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1418                  const CallArgList &CallArgs,
1419                  const ObjCMethodDecl *Method) {
1420    if (!NullBB) return result;
1421
1422    llvm::Value *NullInitPtr = 0;
1423    if (result.isScalar() && !resultType->isVoidType()) {
1424      NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1425      CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1426    }
1427
1428    // Finish the call path.
1429    llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1430    if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1431
1432    // Emit the null-init block and perform the null-initialization there.
1433    CGF.EmitBlock(NullBB);
1434
1435    // Release consumed arguments along the null-receiver path.
1436    if (Method) {
1437      CallArgList::const_iterator I = CallArgs.begin();
1438      for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1439           e = Method->param_end(); i != e; ++i, ++I) {
1440        const ParmVarDecl *ParamDecl = (*i);
1441        if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1442          RValue RV = I->RV;
1443          assert(RV.isScalar() &&
1444                 "NullReturnState::complete - arg not on object");
1445          CGF.EmitARCRelease(RV.getScalarVal(), true);
1446        }
1447      }
1448    }
1449
1450    if (result.isScalar()) {
1451      if (NullInitPtr)
1452        CGF.EmitNullInitialization(NullInitPtr, resultType);
1453      // Jump to the continuation block.
1454      CGF.EmitBlock(contBB);
1455      return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1456      : result;
1457    }
1458
1459    if (!resultType->isAnyComplexType()) {
1460      assert(result.isAggregate() && "null init of non-aggregate result?");
1461      CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1462      // Jump to the continuation block.
1463      CGF.EmitBlock(contBB);
1464      return result;
1465    }
1466
1467    // _Complex type
1468    // FIXME. Now easy to handle any other scalar type whose result is returned
1469    // in memory due to ABI limitations.
1470    CGF.EmitBlock(contBB);
1471    CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1472    llvm::Type *MemberType = CallCV.first->getType();
1473    llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1474    // Create phi instruction for scalar complex value.
1475    llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1476    PHIReal->addIncoming(ZeroCV, NullBB);
1477    PHIReal->addIncoming(CallCV.first, callBB);
1478    llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1479    PHIImag->addIncoming(ZeroCV, NullBB);
1480    PHIImag->addIncoming(CallCV.second, callBB);
1481    return RValue::getComplex(PHIReal, PHIImag);
1482  }
1483};
1484
1485} // end anonymous namespace
1486
1487/* *** Helper Functions *** */
1488
1489/// getConstantGEP() - Help routine to construct simple GEPs.
1490static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1491                                      llvm::Constant *C,
1492                                      unsigned idx0,
1493                                      unsigned idx1) {
1494  llvm::Value *Idxs[] = {
1495    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1496    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
1497  };
1498  return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
1499}
1500
1501/// hasObjCExceptionAttribute - Return true if this class or any super
1502/// class has the __objc_exception__ attribute.
1503static bool hasObjCExceptionAttribute(ASTContext &Context,
1504                                      const ObjCInterfaceDecl *OID) {
1505  if (OID->hasAttr<ObjCExceptionAttr>())
1506    return true;
1507  if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1508    return hasObjCExceptionAttribute(Context, Super);
1509  return false;
1510}
1511
1512/* *** CGObjCMac Public Interface *** */
1513
1514CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1515                                                    ObjCTypes(cgm) {
1516  ObjCABI = 1;
1517  EmitImageInfo();
1518}
1519
1520/// GetClass - Return a reference to the class for the given interface
1521/// decl.
1522llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
1523                                 const ObjCInterfaceDecl *ID) {
1524  return EmitClassRef(Builder, ID);
1525}
1526
1527/// GetSelector - Return the pointer to the unique'd string for this selector.
1528llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1529                                    bool lval) {
1530  return EmitSelector(Builder, Sel, lval);
1531}
1532llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1533                                    *Method) {
1534  return EmitSelector(Builder, Method->getSelector());
1535}
1536
1537llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1538  if (T->isObjCIdType() ||
1539      T->isObjCQualifiedIdType()) {
1540    return CGM.GetAddrOfRTTIDescriptor(
1541              CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
1542  }
1543  if (T->isObjCClassType() ||
1544      T->isObjCQualifiedClassType()) {
1545    return CGM.GetAddrOfRTTIDescriptor(
1546             CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
1547  }
1548  if (T->isObjCObjectPointerType())
1549    return CGM.GetAddrOfRTTIDescriptor(T,  /*ForEH=*/true);
1550
1551  llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1552}
1553
1554/// Generate a constant CFString object.
1555/*
1556  struct __builtin_CFString {
1557  const int *isa; // point to __CFConstantStringClassReference
1558  int flags;
1559  const char *str;
1560  long length;
1561  };
1562*/
1563
1564/// or Generate a constant NSString object.
1565/*
1566   struct __builtin_NSString {
1567     const int *isa; // point to __NSConstantStringClassReference
1568     const char *str;
1569     unsigned int length;
1570   };
1571*/
1572
1573llvm::Constant *CGObjCCommonMac::GenerateConstantString(
1574  const StringLiteral *SL) {
1575  return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1576          CGM.GetAddrOfConstantCFString(SL) :
1577          CGM.GetAddrOfConstantString(SL));
1578}
1579
1580/// Generates a message send where the super is the receiver.  This is
1581/// a message send to self with special delivery semantics indicating
1582/// which class's method should be called.
1583CodeGen::RValue
1584CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1585                                    ReturnValueSlot Return,
1586                                    QualType ResultType,
1587                                    Selector Sel,
1588                                    const ObjCInterfaceDecl *Class,
1589                                    bool isCategoryImpl,
1590                                    llvm::Value *Receiver,
1591                                    bool IsClassMessage,
1592                                    const CodeGen::CallArgList &CallArgs,
1593                                    const ObjCMethodDecl *Method) {
1594  // Create and init a super structure; this is a (receiver, class)
1595  // pair we will pass to objc_msgSendSuper.
1596  llvm::Value *ObjCSuper =
1597    CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
1598  llvm::Value *ReceiverAsObject =
1599    CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1600  CGF.Builder.CreateStore(ReceiverAsObject,
1601                          CGF.Builder.CreateStructGEP(ObjCSuper, 0));
1602
1603  // If this is a class message the metaclass is passed as the target.
1604  llvm::Value *Target;
1605  if (IsClassMessage) {
1606    if (isCategoryImpl) {
1607      // Message sent to 'super' in a class method defined in a category
1608      // implementation requires an odd treatment.
1609      // If we are in a class method, we must retrieve the
1610      // _metaclass_ for the current class, pointed at by
1611      // the class's "isa" pointer.  The following assumes that
1612      // isa" is the first ivar in a class (which it must be).
1613      Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1614      Target = CGF.Builder.CreateStructGEP(Target, 0);
1615      Target = CGF.Builder.CreateLoad(Target);
1616    } else {
1617      llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1618      llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1619      llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1620      Target = Super;
1621    }
1622  }
1623  else if (isCategoryImpl)
1624    Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1625  else {
1626    llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1627    ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1628    Target = CGF.Builder.CreateLoad(ClassPtr);
1629  }
1630  // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1631  // ObjCTypes types.
1632  llvm::Type *ClassTy =
1633    CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
1634  Target = CGF.Builder.CreateBitCast(Target, ClassTy);
1635  CGF.Builder.CreateStore(Target,
1636                          CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1637  return EmitMessageSend(CGF, Return, ResultType,
1638                         EmitSelector(CGF.Builder, Sel),
1639                         ObjCSuper, ObjCTypes.SuperPtrCTy,
1640                         true, CallArgs, Method, ObjCTypes);
1641}
1642
1643/// Generate code for a message send expression.
1644CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1645                                               ReturnValueSlot Return,
1646                                               QualType ResultType,
1647                                               Selector Sel,
1648                                               llvm::Value *Receiver,
1649                                               const CallArgList &CallArgs,
1650                                               const ObjCInterfaceDecl *Class,
1651                                               const ObjCMethodDecl *Method) {
1652  return EmitMessageSend(CGF, Return, ResultType,
1653                         EmitSelector(CGF.Builder, Sel),
1654                         Receiver, CGF.getContext().getObjCIdType(),
1655                         false, CallArgs, Method, ObjCTypes);
1656}
1657
1658CodeGen::RValue
1659CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1660                                 ReturnValueSlot Return,
1661                                 QualType ResultType,
1662                                 llvm::Value *Sel,
1663                                 llvm::Value *Arg0,
1664                                 QualType Arg0Ty,
1665                                 bool IsSuper,
1666                                 const CallArgList &CallArgs,
1667                                 const ObjCMethodDecl *Method,
1668                                 const ObjCCommonTypesHelper &ObjCTypes) {
1669  CallArgList ActualArgs;
1670  if (!IsSuper)
1671    Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
1672  ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1673  ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
1674  ActualArgs.addFrom(CallArgs);
1675
1676  CodeGenTypes &Types = CGM.getTypes();
1677  const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
1678                                                       FunctionType::ExtInfo());
1679  llvm::FunctionType *FTy =
1680    Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1681
1682  if (Method)
1683    assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1684           CGM.getContext().getCanonicalType(ResultType) &&
1685           "Result type mismatch!");
1686
1687  NullReturnState nullReturn;
1688
1689  llvm::Constant *Fn = NULL;
1690  if (CGM.ReturnTypeUsesSRet(FnInfo)) {
1691    if (!IsSuper) nullReturn.init(CGF, Arg0);
1692    Fn = (ObjCABI == 2) ?  ObjCTypes.getSendStretFn2(IsSuper)
1693      : ObjCTypes.getSendStretFn(IsSuper);
1694  } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1695    Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1696      : ObjCTypes.getSendFpretFn(IsSuper);
1697  } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1698    Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1699      : ObjCTypes.getSendFp2retFn(IsSuper);
1700  } else {
1701    Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1702      : ObjCTypes.getSendFn(IsSuper);
1703  }
1704
1705  bool requiresnullCheck = false;
1706  if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1707    for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1708         e = Method->param_end(); i != e; ++i) {
1709      const ParmVarDecl *ParamDecl = (*i);
1710      if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1711        if (!nullReturn.NullBB)
1712          nullReturn.init(CGF, Arg0);
1713        requiresnullCheck = true;
1714        break;
1715      }
1716    }
1717
1718  Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
1719  RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1720  return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1721                             requiresnullCheck ? Method : 0);
1722}
1723
1724static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1725  if (FQT.isObjCGCStrong())
1726    return Qualifiers::Strong;
1727
1728  if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
1729    return Qualifiers::Weak;
1730
1731  if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1732    return Qualifiers::Strong;
1733
1734  if (const PointerType *PT = FQT->getAs<PointerType>())
1735    return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1736
1737  return Qualifiers::GCNone;
1738}
1739
1740llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1741                                                const CGBlockInfo &blockInfo) {
1742  llvm::Constant *nullPtr =
1743    llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
1744
1745  if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
1746      !CGM.getLangOptions().ObjCAutoRefCount)
1747    return nullPtr;
1748
1749  bool hasUnion = false;
1750  SkipIvars.clear();
1751  IvarsInfo.clear();
1752  unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1753  unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
1754
1755  // __isa is the first field in block descriptor and must assume by runtime's
1756  // convention that it is GC'able.
1757  IvarsInfo.push_back(GC_IVAR(0, 1));
1758
1759  const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1760
1761  // Calculate the basic layout of the block structure.
1762  const llvm::StructLayout *layout =
1763    CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1764
1765  // Ignore the optional 'this' capture: C++ objects are not assumed
1766  // to be GC'ed.
1767
1768  // Walk the captured variables.
1769  for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1770         ce = blockDecl->capture_end(); ci != ce; ++ci) {
1771    const VarDecl *variable = ci->getVariable();
1772    QualType type = variable->getType();
1773
1774    const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1775
1776    // Ignore constant captures.
1777    if (capture.isConstant()) continue;
1778
1779    uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1780
1781    // __block variables are passed by their descriptor address.
1782    if (ci->isByRef()) {
1783      IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
1784      continue;
1785    }
1786
1787    assert(!type->isArrayType() && "array variable should not be caught");
1788    if (const RecordType *record = type->getAs<RecordType>()) {
1789      BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
1790      continue;
1791    }
1792
1793    Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1794    unsigned fieldSize = CGM.getContext().getTypeSize(type);
1795
1796    if (GCAttr == Qualifiers::Strong)
1797      IvarsInfo.push_back(GC_IVAR(fieldOffset,
1798                                  fieldSize / WordSizeInBits));
1799    else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1800      SkipIvars.push_back(GC_IVAR(fieldOffset,
1801                                  fieldSize / ByteSizeInBits));
1802  }
1803
1804  if (IvarsInfo.empty())
1805    return nullPtr;
1806
1807  // Sort on byte position; captures might not be allocated in order,
1808  // and unions can do funny things.
1809  llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1810  llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
1811
1812  std::string BitMap;
1813  llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
1814  if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1815    printf("\n block variable layout for block: ");
1816    const unsigned char *s = (unsigned char*)BitMap.c_str();
1817    for (unsigned i = 0; i < BitMap.size(); i++)
1818      if (!(s[i] & 0xf0))
1819        printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1820      else
1821        printf("0x%x%s",  s[i], s[i] != 0 ? ", " : "");
1822    printf("\n");
1823  }
1824
1825  return C;
1826}
1827
1828llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
1829                                            const ObjCProtocolDecl *PD) {
1830  // FIXME: I don't understand why gcc generates this, or where it is
1831  // resolved. Investigate. Its also wasteful to look this up over and over.
1832  LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1833
1834  return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1835                                        ObjCTypes.getExternalProtocolPtrTy());
1836}
1837
1838void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
1839  // FIXME: We shouldn't need this, the protocol decl should contain enough
1840  // information to tell us whether this was a declaration or a definition.
1841  DefinedProtocols.insert(PD->getIdentifier());
1842
1843  // If we have generated a forward reference to this protocol, emit
1844  // it now. Otherwise do nothing, the protocol objects are lazily
1845  // emitted.
1846  if (Protocols.count(PD->getIdentifier()))
1847    GetOrEmitProtocol(PD);
1848}
1849
1850llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
1851  if (DefinedProtocols.count(PD->getIdentifier()))
1852    return GetOrEmitProtocol(PD);
1853
1854  return GetOrEmitProtocolRef(PD);
1855}
1856
1857/*
1858// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1859struct _objc_protocol {
1860struct _objc_protocol_extension *isa;
1861char *protocol_name;
1862struct _objc_protocol_list *protocol_list;
1863struct _objc__method_prototype_list *instance_methods;
1864struct _objc__method_prototype_list *class_methods
1865};
1866
1867See EmitProtocolExtension().
1868*/
1869llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1870  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1871
1872  // Early exit if a defining object has already been generated.
1873  if (Entry && Entry->hasInitializer())
1874    return Entry;
1875
1876  // Use the protocol definition, if there is one.
1877  if (const ObjCProtocolDecl *Def = PD->getDefinition())
1878    PD = Def;
1879
1880  // FIXME: I don't understand why gcc generates this, or where it is
1881  // resolved. Investigate. Its also wasteful to look this up over and over.
1882  LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1883
1884  // Construct method lists.
1885  std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1886  std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
1887  std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
1888  for (ObjCProtocolDecl::instmeth_iterator
1889         i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
1890    ObjCMethodDecl *MD = *i;
1891    llvm::Constant *C = GetMethodDescriptionConstant(MD);
1892    if (!C)
1893      return GetOrEmitProtocolRef(PD);
1894
1895    if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1896      OptInstanceMethods.push_back(C);
1897      OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
1898    } else {
1899      InstanceMethods.push_back(C);
1900      MethodTypesExt.push_back(GetMethodVarType(MD, true));
1901    }
1902  }
1903
1904  for (ObjCProtocolDecl::classmeth_iterator
1905         i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
1906    ObjCMethodDecl *MD = *i;
1907    llvm::Constant *C = GetMethodDescriptionConstant(MD);
1908    if (!C)
1909      return GetOrEmitProtocolRef(PD);
1910
1911    if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1912      OptClassMethods.push_back(C);
1913      OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
1914    } else {
1915      ClassMethods.push_back(C);
1916      MethodTypesExt.push_back(GetMethodVarType(MD, true));
1917    }
1918  }
1919
1920  MethodTypesExt.insert(MethodTypesExt.end(),
1921                        OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1922
1923  llvm::Constant *Values[] = {
1924    EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1925                          MethodTypesExt),
1926    GetClassName(PD->getIdentifier()),
1927    EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
1928                     PD->protocol_begin(),
1929                     PD->protocol_end()),
1930    EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
1931                       "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1932                       InstanceMethods),
1933    EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
1934                       "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1935                       ClassMethods)
1936  };
1937  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1938                                                   Values);
1939
1940  if (Entry) {
1941    // Already created, fix the linkage and update the initializer.
1942    Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
1943    Entry->setInitializer(Init);
1944  } else {
1945    Entry =
1946      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
1947                               llvm::GlobalValue::InternalLinkage,
1948                               Init,
1949                               "\01L_OBJC_PROTOCOL_" + PD->getName());
1950    Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1951    // FIXME: Is this necessary? Why only for protocol?
1952    Entry->setAlignment(4);
1953  }
1954  CGM.AddUsedGlobal(Entry);
1955
1956  return Entry;
1957}
1958
1959llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
1960  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1961
1962  if (!Entry) {
1963    // We use the initializer as a marker of whether this is a forward
1964    // reference or not. At module finalization we add the empty
1965    // contents for protocols which were referenced but never defined.
1966    Entry =
1967      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
1968                               llvm::GlobalValue::ExternalLinkage,
1969                               0,
1970                               "\01L_OBJC_PROTOCOL_" + PD->getName());
1971    Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1972    // FIXME: Is this necessary? Why only for protocol?
1973    Entry->setAlignment(4);
1974  }
1975
1976  return Entry;
1977}
1978
1979/*
1980  struct _objc_protocol_extension {
1981  uint32_t size;
1982  struct objc_method_description_list *optional_instance_methods;
1983  struct objc_method_description_list *optional_class_methods;
1984  struct objc_property_list *instance_properties;
1985  const char ** extendedMethodTypes;
1986  };
1987*/
1988llvm::Constant *
1989CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1990                                 const ConstantVector &OptInstanceMethods,
1991                                 const ConstantVector &OptClassMethods,
1992                                 const ConstantVector &MethodTypesExt) {
1993  uint64_t Size =
1994    CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
1995  llvm::Constant *Values[] = {
1996    llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
1997    EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1998                       + PD->getName(),
1999                       "__OBJC,__cat_inst_meth,regular,no_dead_strip",
2000                       OptInstanceMethods),
2001    EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
2002                       "__OBJC,__cat_cls_meth,regular,no_dead_strip",
2003                       OptClassMethods),
2004    EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
2005                     ObjCTypes),
2006    EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2007                            MethodTypesExt, ObjCTypes)
2008  };
2009
2010  // Return null if no extension bits are used.
2011  if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
2012      Values[3]->isNullValue() && Values[4]->isNullValue())
2013    return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2014
2015  llvm::Constant *Init =
2016    llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
2017
2018  // No special section, but goes in llvm.used
2019  return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
2020                           Init,
2021                           0, 0, true);
2022}
2023
2024/*
2025  struct objc_protocol_list {
2026  struct objc_protocol_list *next;
2027  long count;
2028  Protocol *list[];
2029  };
2030*/
2031llvm::Constant *
2032CGObjCMac::EmitProtocolList(Twine Name,
2033                            ObjCProtocolDecl::protocol_iterator begin,
2034                            ObjCProtocolDecl::protocol_iterator end) {
2035  std::vector<llvm::Constant*> ProtocolRefs;
2036
2037  for (; begin != end; ++begin)
2038    ProtocolRefs.push_back(GetProtocolRef(*begin));
2039
2040  // Just return null for empty protocol lists
2041  if (ProtocolRefs.empty())
2042    return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2043
2044  // This list is null terminated.
2045  ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
2046
2047  llvm::Constant *Values[3];
2048  // This field is only used by the runtime.
2049  Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2050  Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
2051                                     ProtocolRefs.size() - 1);
2052  Values[2] =
2053    llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2054                                                  ProtocolRefs.size()),
2055                             ProtocolRefs);
2056
2057  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2058  llvm::GlobalVariable *GV =
2059    CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
2060                      4, false);
2061  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
2062}
2063
2064void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
2065                                   std::vector<llvm::Constant*> &Properties,
2066                                   const Decl *Container,
2067                                   const ObjCProtocolDecl *PROTO,
2068                                   const ObjCCommonTypesHelper &ObjCTypes) {
2069  for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2070         E = PROTO->protocol_end(); P != E; ++P)
2071    PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2072  for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2073       E = PROTO->prop_end(); I != E; ++I) {
2074    const ObjCPropertyDecl *PD = *I;
2075    if (!PropertySet.insert(PD->getIdentifier()))
2076      continue;
2077    llvm::Constant *Prop[] = {
2078      GetPropertyName(PD->getIdentifier()),
2079      GetPropertyTypeString(PD, Container)
2080    };
2081    Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2082  }
2083}
2084
2085/*
2086  struct _objc_property {
2087  const char * const name;
2088  const char * const attributes;
2089  };
2090
2091  struct _objc_property_list {
2092  uint32_t entsize; // sizeof (struct _objc_property)
2093  uint32_t prop_count;
2094  struct _objc_property[prop_count];
2095  };
2096*/
2097llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
2098                                       const Decl *Container,
2099                                       const ObjCContainerDecl *OCD,
2100                                       const ObjCCommonTypesHelper &ObjCTypes) {
2101  std::vector<llvm::Constant*> Properties;
2102  llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
2103  for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2104         E = OCD->prop_end(); I != E; ++I) {
2105    const ObjCPropertyDecl *PD = *I;
2106    PropertySet.insert(PD->getIdentifier());
2107    llvm::Constant *Prop[] = {
2108      GetPropertyName(PD->getIdentifier()),
2109      GetPropertyTypeString(PD, Container)
2110    };
2111    Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
2112                                                   Prop));
2113  }
2114  if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
2115    for (ObjCInterfaceDecl::all_protocol_iterator
2116         P = OID->all_referenced_protocol_begin(),
2117         E = OID->all_referenced_protocol_end(); P != E; ++P)
2118      PushProtocolProperties(PropertySet, Properties, Container, (*P),
2119                             ObjCTypes);
2120  }
2121  else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2122    for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2123         E = CD->protocol_end(); P != E; ++P)
2124      PushProtocolProperties(PropertySet, Properties, Container, (*P),
2125                             ObjCTypes);
2126  }
2127
2128  // Return null for empty list.
2129  if (Properties.empty())
2130    return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
2131
2132  unsigned PropertySize =
2133    CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
2134  llvm::Constant *Values[3];
2135  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2136  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
2137  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
2138                                             Properties.size());
2139  Values[2] = llvm::ConstantArray::get(AT, Properties);
2140  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2141
2142  llvm::GlobalVariable *GV =
2143    CreateMetadataVar(Name, Init,
2144                      (ObjCABI == 2) ? "__DATA, __objc_const" :
2145                      "__OBJC,__property,regular,no_dead_strip",
2146                      (ObjCABI == 2) ? 8 : 4,
2147                      true);
2148  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
2149}
2150
2151llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2152                                      const ConstantVector &MethodTypes,
2153                                      const ObjCCommonTypesHelper &ObjCTypes) {
2154  // Return null for empty list.
2155  if (MethodTypes.empty())
2156    return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2157
2158  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2159                                             MethodTypes.size());
2160  llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2161
2162  llvm::GlobalVariable *GV =
2163    CreateMetadataVar(Name, Init,
2164                      (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2165                      (ObjCABI == 2) ? 8 : 4,
2166                      true);
2167  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2168}
2169
2170/*
2171  struct objc_method_description_list {
2172  int count;
2173  struct objc_method_description list[];
2174  };
2175*/
2176llvm::Constant *
2177CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2178  llvm::Constant *Desc[] = {
2179    llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2180                                   ObjCTypes.SelectorPtrTy),
2181    GetMethodVarType(MD)
2182  };
2183  if (!Desc[1])
2184    return 0;
2185
2186  return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
2187                                   Desc);
2188}
2189
2190llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
2191                                              const char *Section,
2192                                              const ConstantVector &Methods) {
2193  // Return null for empty list.
2194  if (Methods.empty())
2195    return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2196
2197  llvm::Constant *Values[2];
2198  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2199  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
2200                                             Methods.size());
2201  Values[1] = llvm::ConstantArray::get(AT, Methods);
2202  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2203
2204  llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
2205  return llvm::ConstantExpr::getBitCast(GV,
2206                                        ObjCTypes.MethodDescriptionListPtrTy);
2207}
2208
2209/*
2210  struct _objc_category {
2211  char *category_name;
2212  char *class_name;
2213  struct _objc_method_list *instance_methods;
2214  struct _objc_method_list *class_methods;
2215  struct _objc_protocol_list *protocols;
2216  uint32_t size; // <rdar://4585769>
2217  struct _objc_property_list *instance_properties;
2218  };
2219*/
2220void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
2221  unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
2222
2223  // FIXME: This is poor design, the OCD should have a pointer to the category
2224  // decl. Additionally, note that Category can be null for the @implementation
2225  // w/o an @interface case. Sema should just create one for us as it does for
2226  // @implementation so everyone else can live life under a clear blue sky.
2227  const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
2228  const ObjCCategoryDecl *Category =
2229    Interface->FindCategoryDeclaration(OCD->getIdentifier());
2230
2231  llvm::SmallString<256> ExtName;
2232  llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2233                                     << OCD->getName();
2234
2235  std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2236  for (ObjCCategoryImplDecl::instmeth_iterator
2237         i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
2238    // Instance methods should always be defined.
2239    InstanceMethods.push_back(GetMethodConstant(*i));
2240  }
2241  for (ObjCCategoryImplDecl::classmeth_iterator
2242         i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
2243    // Class methods should always be defined.
2244    ClassMethods.push_back(GetMethodConstant(*i));
2245  }
2246
2247  llvm::Constant *Values[7];
2248  Values[0] = GetClassName(OCD->getIdentifier());
2249  Values[1] = GetClassName(Interface->getIdentifier());
2250  LazySymbols.insert(Interface->getIdentifier());
2251  Values[2] =
2252    EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
2253                   "__OBJC,__cat_inst_meth,regular,no_dead_strip",
2254                   InstanceMethods);
2255  Values[3] =
2256    EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
2257                   "__OBJC,__cat_cls_meth,regular,no_dead_strip",
2258                   ClassMethods);
2259  if (Category) {
2260    Values[4] =
2261      EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
2262                       Category->protocol_begin(),
2263                       Category->protocol_end());
2264  } else {
2265    Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2266  }
2267  Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
2268
2269  // If there is no category @interface then there can be no properties.
2270  if (Category) {
2271    Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
2272                                 OCD, Category, ObjCTypes);
2273  } else {
2274    Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
2275  }
2276
2277  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
2278                                                   Values);
2279
2280  llvm::GlobalVariable *GV =
2281    CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
2282                      "__OBJC,__category,regular,no_dead_strip",
2283                      4, true);
2284  DefinedCategories.push_back(GV);
2285  DefinedCategoryNames.insert(ExtName.str());
2286  // method definition entries must be clear for next implementation.
2287  MethodDefinitions.clear();
2288}
2289
2290// FIXME: Get from somewhere?
2291enum ClassFlags {
2292  eClassFlags_Factory              = 0x00001,
2293  eClassFlags_Meta                 = 0x00002,
2294  // <rdr://5142207>
2295  eClassFlags_HasCXXStructors      = 0x02000,
2296  eClassFlags_Hidden               = 0x20000,
2297  eClassFlags_ABI2_Hidden          = 0x00010,
2298  eClassFlags_ABI2_HasCXXStructors = 0x00004   // <rdr://4923634>
2299};
2300
2301/*
2302  struct _objc_class {
2303  Class isa;
2304  Class super_class;
2305  const char *name;
2306  long version;
2307  long info;
2308  long instance_size;
2309  struct _objc_ivar_list *ivars;
2310  struct _objc_method_list *methods;
2311  struct _objc_cache *cache;
2312  struct _objc_protocol_list *protocols;
2313  // Objective-C 1.0 extensions (<rdr://4585769>)
2314  const char *ivar_layout;
2315  struct _objc_class_ext *ext;
2316  };
2317
2318  See EmitClassExtension();
2319*/
2320void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
2321  DefinedSymbols.insert(ID->getIdentifier());
2322
2323  std::string ClassName = ID->getNameAsString();
2324  // FIXME: Gross
2325  ObjCInterfaceDecl *Interface =
2326    const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
2327  llvm::Constant *Protocols =
2328    EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
2329                     Interface->all_referenced_protocol_begin(),
2330                     Interface->all_referenced_protocol_end());
2331  unsigned Flags = eClassFlags_Factory;
2332  if (ID->hasCXXStructors())
2333    Flags |= eClassFlags_HasCXXStructors;
2334  unsigned Size =
2335    CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
2336
2337  // FIXME: Set CXX-structors flag.
2338  if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
2339    Flags |= eClassFlags_Hidden;
2340
2341  std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2342  for (ObjCImplementationDecl::instmeth_iterator
2343         i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
2344    // Instance methods should always be defined.
2345    InstanceMethods.push_back(GetMethodConstant(*i));
2346  }
2347  for (ObjCImplementationDecl::classmeth_iterator
2348         i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
2349    // Class methods should always be defined.
2350    ClassMethods.push_back(GetMethodConstant(*i));
2351  }
2352
2353  for (ObjCImplementationDecl::propimpl_iterator
2354         i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
2355    ObjCPropertyImplDecl *PID = *i;
2356
2357    if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2358      ObjCPropertyDecl *PD = PID->getPropertyDecl();
2359
2360      if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2361        if (llvm::Constant *C = GetMethodConstant(MD))
2362          InstanceMethods.push_back(C);
2363      if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2364        if (llvm::Constant *C = GetMethodConstant(MD))
2365          InstanceMethods.push_back(C);
2366    }
2367  }
2368
2369  llvm::Constant *Values[12];
2370  Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
2371  if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
2372    // Record a reference to the super class.
2373    LazySymbols.insert(Super->getIdentifier());
2374
2375    Values[ 1] =
2376      llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2377                                     ObjCTypes.ClassPtrTy);
2378  } else {
2379    Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2380  }
2381  Values[ 2] = GetClassName(ID->getIdentifier());
2382  // Version is always 0.
2383  Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2384  Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2385  Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
2386  Values[ 6] = EmitIvarList(ID, false);
2387  Values[ 7] =
2388    EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
2389                   "__OBJC,__inst_meth,regular,no_dead_strip",
2390                   InstanceMethods);
2391  // cache is always NULL.
2392  Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2393  Values[ 9] = Protocols;
2394  Values[10] = BuildIvarLayout(ID, true);
2395  Values[11] = EmitClassExtension(ID);
2396  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2397                                                   Values);
2398  std::string Name("\01L_OBJC_CLASS_");
2399  Name += ClassName;
2400  const char *Section = "__OBJC,__class,regular,no_dead_strip";
2401  // Check for a forward reference.
2402  llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2403  if (GV) {
2404    assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2405           "Forward metaclass reference has incorrect type.");
2406    GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2407    GV->setInitializer(Init);
2408    GV->setSection(Section);
2409    GV->setAlignment(4);
2410    CGM.AddUsedGlobal(GV);
2411  }
2412  else
2413    GV = CreateMetadataVar(Name, Init, Section, 4, true);
2414  DefinedClasses.push_back(GV);
2415  // method definition entries must be clear for next implementation.
2416  MethodDefinitions.clear();
2417}
2418
2419llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2420                                         llvm::Constant *Protocols,
2421                                         const ConstantVector &Methods) {
2422  unsigned Flags = eClassFlags_Meta;
2423  unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
2424
2425  if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
2426    Flags |= eClassFlags_Hidden;
2427
2428  llvm::Constant *Values[12];
2429  // The isa for the metaclass is the root of the hierarchy.
2430  const ObjCInterfaceDecl *Root = ID->getClassInterface();
2431  while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2432    Root = Super;
2433  Values[ 0] =
2434    llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
2435                                   ObjCTypes.ClassPtrTy);
2436  // The super class for the metaclass is emitted as the name of the
2437  // super class. The runtime fixes this up to point to the
2438  // *metaclass* for the super class.
2439  if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2440    Values[ 1] =
2441      llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2442                                     ObjCTypes.ClassPtrTy);
2443  } else {
2444    Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2445  }
2446  Values[ 2] = GetClassName(ID->getIdentifier());
2447  // Version is always 0.
2448  Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2449  Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2450  Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
2451  Values[ 6] = EmitIvarList(ID, true);
2452  Values[ 7] =
2453    EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
2454                   "__OBJC,__cls_meth,regular,no_dead_strip",
2455                   Methods);
2456  // cache is always NULL.
2457  Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2458  Values[ 9] = Protocols;
2459  // ivar_layout for metaclass is always NULL.
2460  Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2461  // The class extension is always unused for metaclasses.
2462  Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2463  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2464                                                   Values);
2465
2466  std::string Name("\01L_OBJC_METACLASS_");
2467  Name += ID->getNameAsCString();
2468
2469  // Check for a forward reference.
2470  llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2471  if (GV) {
2472    assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2473           "Forward metaclass reference has incorrect type.");
2474    GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2475    GV->setInitializer(Init);
2476  } else {
2477    GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2478                                  llvm::GlobalValue::InternalLinkage,
2479                                  Init, Name);
2480  }
2481  GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
2482  GV->setAlignment(4);
2483  CGM.AddUsedGlobal(GV);
2484
2485  return GV;
2486}
2487
2488llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
2489  std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
2490
2491  // FIXME: Should we look these up somewhere other than the module. Its a bit
2492  // silly since we only generate these while processing an implementation, so
2493  // exactly one pointer would work if know when we entered/exitted an
2494  // implementation block.
2495
2496  // Check for an existing forward reference.
2497  // Previously, metaclass with internal linkage may have been defined.
2498  // pass 'true' as 2nd argument so it is returned.
2499  if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2500                                                                   true)) {
2501    assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2502           "Forward metaclass reference has incorrect type.");
2503    return GV;
2504  } else {
2505    // Generate as an external reference to keep a consistent
2506    // module. This will be patched up when we emit the metaclass.
2507    return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2508                                    llvm::GlobalValue::ExternalLinkage,
2509                                    0,
2510                                    Name);
2511  }
2512}
2513
2514llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2515  std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2516
2517  if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2518                                                                   true)) {
2519    assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2520           "Forward class metadata reference has incorrect type.");
2521    return GV;
2522  } else {
2523    return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2524                                    llvm::GlobalValue::ExternalLinkage,
2525                                    0,
2526                                    Name);
2527  }
2528}
2529
2530/*
2531  struct objc_class_ext {
2532  uint32_t size;
2533  const char *weak_ivar_layout;
2534  struct _objc_property_list *properties;
2535  };
2536*/
2537llvm::Constant *
2538CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2539  uint64_t Size =
2540    CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
2541
2542  llvm::Constant *Values[3];
2543  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
2544  Values[1] = BuildIvarLayout(ID, false);
2545  Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
2546                               ID, ID->getClassInterface(), ObjCTypes);
2547
2548  // Return null if no extension bits are used.
2549  if (Values[1]->isNullValue() && Values[2]->isNullValue())
2550    return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2551
2552  llvm::Constant *Init =
2553    llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
2554  return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
2555                           Init, "__OBJC,__class_ext,regular,no_dead_strip",
2556                           4, true);
2557}
2558
2559/*
2560  struct objc_ivar {
2561  char *ivar_name;
2562  char *ivar_type;
2563  int ivar_offset;
2564  };
2565
2566  struct objc_ivar_list {
2567  int ivar_count;
2568  struct objc_ivar list[count];
2569  };
2570*/
2571llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
2572                                        bool ForClass) {
2573  std::vector<llvm::Constant*> Ivars;
2574
2575  // When emitting the root class GCC emits ivar entries for the
2576  // actual class structure. It is not clear if we need to follow this
2577  // behavior; for now lets try and get away with not doing it. If so,
2578  // the cleanest solution would be to make up an ObjCInterfaceDecl
2579  // for the class.
2580  if (ForClass)
2581    return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2582
2583  const ObjCInterfaceDecl *OID = ID->getClassInterface();
2584
2585  for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
2586       IVD; IVD = IVD->getNextIvar()) {
2587    // Ignore unnamed bit-fields.
2588    if (!IVD->getDeclName())
2589      continue;
2590    llvm::Constant *Ivar[] = {
2591      GetMethodVarName(IVD->getIdentifier()),
2592      GetMethodVarType(IVD),
2593      llvm::ConstantInt::get(ObjCTypes.IntTy,
2594                             ComputeIvarBaseOffset(CGM, OID, IVD))
2595    };
2596    Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
2597  }
2598
2599  // Return null for empty list.
2600  if (Ivars.empty())
2601    return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2602
2603  llvm::Constant *Values[2];
2604  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2605  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2606                                             Ivars.size());
2607  Values[1] = llvm::ConstantArray::get(AT, Ivars);
2608  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2609
2610  llvm::GlobalVariable *GV;
2611  if (ForClass)
2612    GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
2613                           Init, "__OBJC,__class_vars,regular,no_dead_strip",
2614                           4, true);
2615  else
2616    GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
2617                           Init, "__OBJC,__instance_vars,regular,no_dead_strip",
2618                           4, true);
2619  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
2620}
2621
2622/*
2623  struct objc_method {
2624  SEL method_name;
2625  char *method_types;
2626  void *method;
2627  };
2628
2629  struct objc_method_list {
2630  struct objc_method_list *obsolete;
2631  int count;
2632  struct objc_method methods_list[count];
2633  };
2634*/
2635
2636/// GetMethodConstant - Return a struct objc_method constant for the
2637/// given method if it has been defined. The result is null if the
2638/// method has not been defined. The return value has type MethodPtrTy.
2639llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
2640  llvm::Function *Fn = GetMethodDefinition(MD);
2641  if (!Fn)
2642    return 0;
2643
2644  llvm::Constant *Method[] = {
2645    llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2646                                   ObjCTypes.SelectorPtrTy),
2647    GetMethodVarType(MD),
2648    llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2649  };
2650  return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2651}
2652
2653llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
2654                                          const char *Section,
2655                                          const ConstantVector &Methods) {
2656  // Return null for empty list.
2657  if (Methods.empty())
2658    return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2659
2660  llvm::Constant *Values[3];
2661  Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2662  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2663  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2664                                             Methods.size());
2665  Values[2] = llvm::ConstantArray::get(AT, Methods);
2666  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2667
2668  llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
2669  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
2670}
2671
2672llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
2673                                                const ObjCContainerDecl *CD) {
2674  llvm::SmallString<256> Name;
2675  GetNameForMethod(OMD, CD, Name);
2676
2677  CodeGenTypes &Types = CGM.getTypes();
2678  llvm::FunctionType *MethodTy =
2679    Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
2680  llvm::Function *Method =
2681    llvm::Function::Create(MethodTy,
2682                           llvm::GlobalValue::InternalLinkage,
2683                           Name.str(),
2684                           &CGM.getModule());
2685  MethodDefinitions.insert(std::make_pair(OMD, Method));
2686
2687  return Method;
2688}
2689
2690llvm::GlobalVariable *
2691CGObjCCommonMac::CreateMetadataVar(Twine Name,
2692                                   llvm::Constant *Init,
2693                                   const char *Section,
2694                                   unsigned Align,
2695                                   bool AddToUsed) {
2696  llvm::Type *Ty = Init->getType();
2697  llvm::GlobalVariable *GV =
2698    new llvm::GlobalVariable(CGM.getModule(), Ty, false,
2699                             llvm::GlobalValue::InternalLinkage, Init, Name);
2700  if (Section)
2701    GV->setSection(Section);
2702  if (Align)
2703    GV->setAlignment(Align);
2704  if (AddToUsed)
2705    CGM.AddUsedGlobal(GV);
2706  return GV;
2707}
2708
2709llvm::Function *CGObjCMac::ModuleInitFunction() {
2710  // Abuse this interface function as a place to finalize.
2711  FinishModule();
2712  return NULL;
2713}
2714
2715llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
2716  return ObjCTypes.getGetPropertyFn();
2717}
2718
2719llvm::Constant *CGObjCMac::GetPropertySetFunction() {
2720  return ObjCTypes.getSetPropertyFn();
2721}
2722
2723llvm::Constant *CGObjCMac::GetGetStructFunction() {
2724  return ObjCTypes.getCopyStructFn();
2725}
2726llvm::Constant *CGObjCMac::GetSetStructFunction() {
2727  return ObjCTypes.getCopyStructFn();
2728}
2729
2730llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2731  return ObjCTypes.getCppAtomicObjectFunction();
2732}
2733
2734llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
2735  return ObjCTypes.getEnumerationMutationFn();
2736}
2737
2738void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2739  return EmitTryOrSynchronizedStmt(CGF, S);
2740}
2741
2742void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2743                                     const ObjCAtSynchronizedStmt &S) {
2744  return EmitTryOrSynchronizedStmt(CGF, S);
2745}
2746
2747namespace {
2748  struct PerformFragileFinally : EHScopeStack::Cleanup {
2749    const Stmt &S;
2750    llvm::Value *SyncArgSlot;
2751    llvm::Value *CallTryExitVar;
2752    llvm::Value *ExceptionData;
2753    ObjCTypesHelper &ObjCTypes;
2754    PerformFragileFinally(const Stmt *S,
2755                          llvm::Value *SyncArgSlot,
2756                          llvm::Value *CallTryExitVar,
2757                          llvm::Value *ExceptionData,
2758                          ObjCTypesHelper *ObjCTypes)
2759      : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
2760        ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2761
2762    void Emit(CodeGenFunction &CGF, Flags flags) {
2763      // Check whether we need to call objc_exception_try_exit.
2764      // In optimized code, this branch will always be folded.
2765      llvm::BasicBlock *FinallyCallExit =
2766        CGF.createBasicBlock("finally.call_exit");
2767      llvm::BasicBlock *FinallyNoCallExit =
2768        CGF.createBasicBlock("finally.no_call_exit");
2769      CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2770                               FinallyCallExit, FinallyNoCallExit);
2771
2772      CGF.EmitBlock(FinallyCallExit);
2773      CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2774        ->setDoesNotThrow();
2775
2776      CGF.EmitBlock(FinallyNoCallExit);
2777
2778      if (isa<ObjCAtTryStmt>(S)) {
2779        if (const ObjCAtFinallyStmt* FinallyStmt =
2780              cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2781          // Save the current cleanup destination in case there's
2782          // control flow inside the finally statement.
2783          llvm::Value *CurCleanupDest =
2784            CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2785
2786          CGF.EmitStmt(FinallyStmt->getFinallyBody());
2787
2788          if (CGF.HaveInsertPoint()) {
2789            CGF.Builder.CreateStore(CurCleanupDest,
2790                                    CGF.getNormalCleanupDestSlot());
2791          } else {
2792            // Currently, the end of the cleanup must always exist.
2793            CGF.EnsureInsertPoint();
2794          }
2795        }
2796      } else {
2797        // Emit objc_sync_exit(expr); as finally's sole statement for
2798        // @synchronized.
2799        llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
2800        CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2801          ->setDoesNotThrow();
2802      }
2803    }
2804  };
2805
2806  class FragileHazards {
2807    CodeGenFunction &CGF;
2808    SmallVector<llvm::Value*, 20> Locals;
2809    llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2810
2811    llvm::InlineAsm *ReadHazard;
2812    llvm::InlineAsm *WriteHazard;
2813
2814    llvm::FunctionType *GetAsmFnType();
2815
2816    void collectLocals();
2817    void emitReadHazard(CGBuilderTy &Builder);
2818
2819  public:
2820    FragileHazards(CodeGenFunction &CGF);
2821
2822    void emitWriteHazard();
2823    void emitHazardsInNewBlocks();
2824  };
2825}
2826
2827/// Create the fragile-ABI read and write hazards based on the current
2828/// state of the function, which is presumed to be immediately prior
2829/// to a @try block.  These hazards are used to maintain correct
2830/// semantics in the face of optimization and the fragile ABI's
2831/// cavalier use of setjmp/longjmp.
2832FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2833  collectLocals();
2834
2835  if (Locals.empty()) return;
2836
2837  // Collect all the blocks in the function.
2838  for (llvm::Function::iterator
2839         I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2840    BlocksBeforeTry.insert(&*I);
2841
2842  llvm::FunctionType *AsmFnTy = GetAsmFnType();
2843
2844  // Create a read hazard for the allocas.  This inhibits dead-store
2845  // optimizations and forces the values to memory.  This hazard is
2846  // inserted before any 'throwing' calls in the protected scope to
2847  // reflect the possibility that the variables might be read from the
2848  // catch block if the call throws.
2849  {
2850    std::string Constraint;
2851    for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2852      if (I) Constraint += ',';
2853      Constraint += "*m";
2854    }
2855
2856    ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2857  }
2858
2859  // Create a write hazard for the allocas.  This inhibits folding
2860  // loads across the hazard.  This hazard is inserted at the
2861  // beginning of the catch path to reflect the possibility that the
2862  // variables might have been written within the protected scope.
2863  {
2864    std::string Constraint;
2865    for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2866      if (I) Constraint += ',';
2867      Constraint += "=*m";
2868    }
2869
2870    WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2871  }
2872}
2873
2874/// Emit a write hazard at the current location.
2875void FragileHazards::emitWriteHazard() {
2876  if (Locals.empty()) return;
2877
2878  CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
2879}
2880
2881void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2882  assert(!Locals.empty());
2883  Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
2884}
2885
2886/// Emit read hazards in all the protected blocks, i.e. all the blocks
2887/// which have been inserted since the beginning of the try.
2888void FragileHazards::emitHazardsInNewBlocks() {
2889  if (Locals.empty()) return;
2890
2891  CGBuilderTy Builder(CGF.getLLVMContext());
2892
2893  // Iterate through all blocks, skipping those prior to the try.
2894  for (llvm::Function::iterator
2895         FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2896    llvm::BasicBlock &BB = *FI;
2897    if (BlocksBeforeTry.count(&BB)) continue;
2898
2899    // Walk through all the calls in the block.
2900    for (llvm::BasicBlock::iterator
2901           BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2902      llvm::Instruction &I = *BI;
2903
2904      // Ignore instructions that aren't non-intrinsic calls.
2905      // These are the only calls that can possibly call longjmp.
2906      if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2907      if (isa<llvm::IntrinsicInst>(I))
2908        continue;
2909
2910      // Ignore call sites marked nounwind.  This may be questionable,
2911      // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2912      llvm::CallSite CS(&I);
2913      if (CS.doesNotThrow()) continue;
2914
2915      // Insert a read hazard before the call.  This will ensure that
2916      // any writes to the locals are performed before making the
2917      // call.  If the call throws, then this is sufficient to
2918      // guarantee correctness as long as it doesn't also write to any
2919      // locals.
2920      Builder.SetInsertPoint(&BB, BI);
2921      emitReadHazard(Builder);
2922    }
2923  }
2924}
2925
2926static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2927  if (V) S.insert(V);
2928}
2929
2930void FragileHazards::collectLocals() {
2931  // Compute a set of allocas to ignore.
2932  llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2933  addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2934  addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2935
2936  // Collect all the allocas currently in the function.  This is
2937  // probably way too aggressive.
2938  llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2939  for (llvm::BasicBlock::iterator
2940         I = Entry.begin(), E = Entry.end(); I != E; ++I)
2941    if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2942      Locals.push_back(&*I);
2943}
2944
2945llvm::FunctionType *FragileHazards::GetAsmFnType() {
2946  SmallVector<llvm::Type *, 16> tys(Locals.size());
2947  for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2948    tys[i] = Locals[i]->getType();
2949  return llvm::FunctionType::get(CGF.VoidTy, tys, false);
2950}
2951
2952/*
2953
2954  Objective-C setjmp-longjmp (sjlj) Exception Handling
2955  --
2956
2957  A catch buffer is a setjmp buffer plus:
2958    - a pointer to the exception that was caught
2959    - a pointer to the previous exception data buffer
2960    - two pointers of reserved storage
2961  Therefore catch buffers form a stack, with a pointer to the top
2962  of the stack kept in thread-local storage.
2963
2964  objc_exception_try_enter pushes a catch buffer onto the EH stack.
2965  objc_exception_try_exit pops the given catch buffer, which is
2966    required to be the top of the EH stack.
2967  objc_exception_throw pops the top of the EH stack, writes the
2968    thrown exception into the appropriate field, and longjmps
2969    to the setjmp buffer.  It crashes the process (with a printf
2970    and an abort()) if there are no catch buffers on the stack.
2971  objc_exception_extract just reads the exception pointer out of the
2972    catch buffer.
2973
2974  There's no reason an implementation couldn't use a light-weight
2975  setjmp here --- something like __builtin_setjmp, but API-compatible
2976  with the heavyweight setjmp.  This will be more important if we ever
2977  want to implement correct ObjC/C++ exception interactions for the
2978  fragile ABI.
2979
2980  Note that for this use of setjmp/longjmp to be correct, we may need
2981  to mark some local variables volatile: if a non-volatile local
2982  variable is modified between the setjmp and the longjmp, it has
2983  indeterminate value.  For the purposes of LLVM IR, it may be
2984  sufficient to make loads and stores within the @try (to variables
2985  declared outside the @try) volatile.  This is necessary for
2986  optimized correctness, but is not currently being done; this is
2987  being tracked as rdar://problem/8160285
2988
2989  The basic framework for a @try-catch-finally is as follows:
2990  {
2991  objc_exception_data d;
2992  id _rethrow = null;
2993  bool _call_try_exit = true;
2994
2995  objc_exception_try_enter(&d);
2996  if (!setjmp(d.jmp_buf)) {
2997  ... try body ...
2998  } else {
2999  // exception path
3000  id _caught = objc_exception_extract(&d);
3001
3002  // enter new try scope for handlers
3003  if (!setjmp(d.jmp_buf)) {
3004  ... match exception and execute catch blocks ...
3005
3006  // fell off end, rethrow.
3007  _rethrow = _caught;
3008  ... jump-through-finally to finally_rethrow ...
3009  } else {
3010  // exception in catch block
3011  _rethrow = objc_exception_extract(&d);
3012  _call_try_exit = false;
3013  ... jump-through-finally to finally_rethrow ...
3014  }
3015  }
3016  ... jump-through-finally to finally_end ...
3017
3018  finally:
3019  if (_call_try_exit)
3020  objc_exception_try_exit(&d);
3021
3022  ... finally block ....
3023  ... dispatch to finally destination ...
3024
3025  finally_rethrow:
3026  objc_exception_throw(_rethrow);
3027
3028  finally_end:
3029  }
3030
3031  This framework differs slightly from the one gcc uses, in that gcc
3032  uses _rethrow to determine if objc_exception_try_exit should be called
3033  and if the object should be rethrown. This breaks in the face of
3034  throwing nil and introduces unnecessary branches.
3035
3036  We specialize this framework for a few particular circumstances:
3037
3038  - If there are no catch blocks, then we avoid emitting the second
3039  exception handling context.
3040
3041  - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3042  e)) we avoid emitting the code to rethrow an uncaught exception.
3043
3044  - FIXME: If there is no @finally block we can do a few more
3045  simplifications.
3046
3047  Rethrows and Jumps-Through-Finally
3048  --
3049
3050  '@throw;' is supported by pushing the currently-caught exception
3051  onto ObjCEHStack while the @catch blocks are emitted.
3052
3053  Branches through the @finally block are handled with an ordinary
3054  normal cleanup.  We do not register an EH cleanup; fragile-ABI ObjC
3055  exceptions are not compatible with C++ exceptions, and this is
3056  hardly the only place where this will go wrong.
3057
3058  @synchronized(expr) { stmt; } is emitted as if it were:
3059    id synch_value = expr;
3060    objc_sync_enter(synch_value);
3061    @try { stmt; } @finally { objc_sync_exit(synch_value); }
3062*/
3063
3064void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3065                                          const Stmt &S) {
3066  bool isTry = isa<ObjCAtTryStmt>(S);
3067
3068  // A destination for the fall-through edges of the catch handlers to
3069  // jump to.
3070  CodeGenFunction::JumpDest FinallyEnd =
3071    CGF.getJumpDestInCurrentScope("finally.end");
3072
3073  // A destination for the rethrow edge of the catch handlers to jump
3074  // to.
3075  CodeGenFunction::JumpDest FinallyRethrow =
3076    CGF.getJumpDestInCurrentScope("finally.rethrow");
3077
3078  // For @synchronized, call objc_sync_enter(sync.expr). The
3079  // evaluation of the expression must occur before we enter the
3080  // @synchronized.  We can't avoid a temp here because we need the
3081  // value to be preserved.  If the backend ever does liveness
3082  // correctly after setjmp, this will be unnecessary.
3083  llvm::Value *SyncArgSlot = 0;
3084  if (!isTry) {
3085    llvm::Value *SyncArg =
3086      CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3087    SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
3088    CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3089      ->setDoesNotThrow();
3090
3091    SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3092    CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
3093  }
3094
3095  // Allocate memory for the setjmp buffer.  This needs to be kept
3096  // live throughout the try and catch blocks.
3097  llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3098                                                    "exceptiondata.ptr");
3099
3100  // Create the fragile hazards.  Note that this will not capture any
3101  // of the allocas required for exception processing, but will
3102  // capture the current basic block (which extends all the way to the
3103  // setjmp call) as "before the @try".
3104  FragileHazards Hazards(CGF);
3105
3106  // Create a flag indicating whether the cleanup needs to call
3107  // objc_exception_try_exit.  This is true except when
3108  //   - no catches match and we're branching through the cleanup
3109  //     just to rethrow the exception, or
3110  //   - a catch matched and we're falling out of the catch handler.
3111  // The setjmp-safety rule here is that we should always store to this
3112  // variable in a place that dominates the branch through the cleanup
3113  // without passing through any setjmps.
3114  llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
3115                                                     "_call_try_exit");
3116
3117  // A slot containing the exception to rethrow.  Only needed when we
3118  // have both a @catch and a @finally.
3119  llvm::Value *PropagatingExnVar = 0;
3120
3121  // Push a normal cleanup to leave the try scope.
3122  CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
3123                                                 SyncArgSlot,
3124                                                 CallTryExitVar,
3125                                                 ExceptionData,
3126                                                 &ObjCTypes);
3127
3128  // Enter a try block:
3129  //  - Call objc_exception_try_enter to push ExceptionData on top of
3130  //    the EH stack.
3131  CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3132      ->setDoesNotThrow();
3133
3134  //  - Call setjmp on the exception data buffer.
3135  llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3136  llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3137  llvm::Value *SetJmpBuffer =
3138    CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
3139  llvm::CallInst *SetJmpResult =
3140    CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3141  SetJmpResult->setDoesNotThrow();
3142  SetJmpResult->setCanReturnTwice();
3143
3144  // If setjmp returned 0, enter the protected block; otherwise,
3145  // branch to the handler.
3146  llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3147  llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
3148  llvm::Value *DidCatch =
3149    CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3150  CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
3151
3152  // Emit the protected block.
3153  CGF.EmitBlock(TryBlock);
3154  CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
3155  CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
3156                     : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
3157
3158  CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
3159
3160  // Emit the exception handler block.
3161  CGF.EmitBlock(TryHandler);
3162
3163  // Don't optimize loads of the in-scope locals across this point.
3164  Hazards.emitWriteHazard();
3165
3166  // For a @synchronized (or a @try with no catches), just branch
3167  // through the cleanup to the rethrow block.
3168  if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3169    // Tell the cleanup not to re-pop the exit.
3170    CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
3171    CGF.EmitBranchThroughCleanup(FinallyRethrow);
3172
3173  // Otherwise, we have to match against the caught exceptions.
3174  } else {
3175    // Retrieve the exception object.  We may emit multiple blocks but
3176    // nothing can cross this so the value is already in SSA form.
3177    llvm::CallInst *Caught =
3178      CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3179                             ExceptionData, "caught");
3180    Caught->setDoesNotThrow();
3181
3182    // Push the exception to rethrow onto the EH value stack for the
3183    // benefit of any @throws in the handlers.
3184    CGF.ObjCEHValueStack.push_back(Caught);
3185
3186    const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
3187
3188    bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
3189
3190    llvm::BasicBlock *CatchBlock = 0;
3191    llvm::BasicBlock *CatchHandler = 0;
3192    if (HasFinally) {
3193      // Save the currently-propagating exception before
3194      // objc_exception_try_enter clears the exception slot.
3195      PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3196                                               "propagating_exception");
3197      CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3198
3199      // Enter a new exception try block (in case a @catch block
3200      // throws an exception).
3201      CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3202        ->setDoesNotThrow();
3203
3204      llvm::CallInst *SetJmpResult =
3205        CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3206                               "setjmp.result");
3207      SetJmpResult->setDoesNotThrow();
3208      SetJmpResult->setCanReturnTwice();
3209
3210      llvm::Value *Threw =
3211        CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3212
3213      CatchBlock = CGF.createBasicBlock("catch");
3214      CatchHandler = CGF.createBasicBlock("catch_for_catch");
3215      CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3216
3217      CGF.EmitBlock(CatchBlock);
3218    }
3219
3220    CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
3221
3222    // Handle catch list. As a special case we check if everything is
3223    // matched and avoid generating code for falling off the end if
3224    // so.
3225    bool AllMatched = false;
3226    for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3227      const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
3228
3229      const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
3230      const ObjCObjectPointerType *OPT = 0;
3231
3232      // catch(...) always matches.
3233      if (!CatchParam) {
3234        AllMatched = true;
3235      } else {
3236        OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
3237
3238        // catch(id e) always matches under this ABI, since only
3239        // ObjC exceptions end up here in the first place.
3240        // FIXME: For the time being we also match id<X>; this should
3241        // be rejected by Sema instead.
3242        if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
3243          AllMatched = true;
3244      }
3245
3246      // If this is a catch-all, we don't need to test anything.
3247      if (AllMatched) {
3248        CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3249
3250        if (CatchParam) {
3251          CGF.EmitAutoVarDecl(*CatchParam);
3252          assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
3253
3254          // These types work out because ConvertType(id) == i8*.
3255          CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
3256        }
3257
3258        CGF.EmitStmt(CatchStmt->getCatchBody());
3259
3260        // The scope of the catch variable ends right here.
3261        CatchVarCleanups.ForceCleanup();
3262
3263        CGF.EmitBranchThroughCleanup(FinallyEnd);
3264        break;
3265      }
3266
3267      assert(OPT && "Unexpected non-object pointer type in @catch");
3268      const ObjCObjectType *ObjTy = OPT->getObjectType();
3269
3270      // FIXME: @catch (Class c) ?
3271      ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3272      assert(IDecl && "Catch parameter must have Objective-C type!");
3273
3274      // Check if the @catch block matches the exception object.
3275      llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
3276
3277      llvm::CallInst *Match =
3278        CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3279                                Class, Caught, "match");
3280      Match->setDoesNotThrow();
3281
3282      llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3283      llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
3284
3285      CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
3286                               MatchedBlock, NextCatchBlock);
3287
3288      // Emit the @catch block.
3289      CGF.EmitBlock(MatchedBlock);
3290
3291      // Collect any cleanups for the catch variable.  The scope lasts until
3292      // the end of the catch body.
3293      CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3294
3295      CGF.EmitAutoVarDecl(*CatchParam);
3296      assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
3297
3298      // Initialize the catch variable.
3299      llvm::Value *Tmp =
3300        CGF.Builder.CreateBitCast(Caught,
3301                                  CGF.ConvertType(CatchParam->getType()));
3302      CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
3303
3304      CGF.EmitStmt(CatchStmt->getCatchBody());
3305
3306      // We're done with the catch variable.
3307      CatchVarCleanups.ForceCleanup();
3308
3309      CGF.EmitBranchThroughCleanup(FinallyEnd);
3310
3311      CGF.EmitBlock(NextCatchBlock);
3312    }
3313
3314    CGF.ObjCEHValueStack.pop_back();
3315
3316    // If nothing wanted anything to do with the caught exception,
3317    // kill the extract call.
3318    if (Caught->use_empty())
3319      Caught->eraseFromParent();
3320
3321    if (!AllMatched)
3322      CGF.EmitBranchThroughCleanup(FinallyRethrow);
3323
3324    if (HasFinally) {
3325      // Emit the exception handler for the @catch blocks.
3326      CGF.EmitBlock(CatchHandler);
3327
3328      // In theory we might now need a write hazard, but actually it's
3329      // unnecessary because there's no local-accessing code between
3330      // the try's write hazard and here.
3331      //Hazards.emitWriteHazard();
3332
3333      // Extract the new exception and save it to the
3334      // propagating-exception slot.
3335      assert(PropagatingExnVar);
3336      llvm::CallInst *NewCaught =
3337        CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3338                               ExceptionData, "caught");
3339      NewCaught->setDoesNotThrow();
3340      CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3341
3342      // Don't pop the catch handler; the throw already did.
3343      CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
3344      CGF.EmitBranchThroughCleanup(FinallyRethrow);
3345    }
3346  }
3347
3348  // Insert read hazards as required in the new blocks.
3349  Hazards.emitHazardsInNewBlocks();
3350
3351  // Pop the cleanup.
3352  CGF.Builder.restoreIP(TryFallthroughIP);
3353  if (CGF.HaveInsertPoint())
3354    CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
3355  CGF.PopCleanupBlock();
3356  CGF.EmitBlock(FinallyEnd.getBlock(), true);
3357
3358  // Emit the rethrow block.
3359  CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
3360  CGF.EmitBlock(FinallyRethrow.getBlock(), true);
3361  if (CGF.HaveInsertPoint()) {
3362    // If we have a propagating-exception variable, check it.
3363    llvm::Value *PropagatingExn;
3364    if (PropagatingExnVar) {
3365      PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
3366
3367    // Otherwise, just look in the buffer for the exception to throw.
3368    } else {
3369      llvm::CallInst *Caught =
3370        CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3371                               ExceptionData);
3372      Caught->setDoesNotThrow();
3373      PropagatingExn = Caught;
3374    }
3375
3376    CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
3377      ->setDoesNotThrow();
3378    CGF.Builder.CreateUnreachable();
3379  }
3380
3381  CGF.Builder.restoreIP(SavedIP);
3382}
3383
3384void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
3385                              const ObjCAtThrowStmt &S) {
3386  llvm::Value *ExceptionAsObject;
3387
3388  if (const Expr *ThrowExpr = S.getThrowExpr()) {
3389    llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
3390    ExceptionAsObject =
3391      CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
3392  } else {
3393    assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
3394           "Unexpected rethrow outside @catch block.");
3395    ExceptionAsObject = CGF.ObjCEHValueStack.back();
3396  }
3397
3398  CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3399    ->setDoesNotReturn();
3400  CGF.Builder.CreateUnreachable();
3401
3402  // Clear the insertion point to indicate we are in unreachable code.
3403  CGF.Builder.ClearInsertionPoint();
3404}
3405
3406/// EmitObjCWeakRead - Code gen for loading value of a __weak
3407/// object: objc_read_weak (id *src)
3408///
3409llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
3410                                          llvm::Value *AddrWeakObj) {
3411  llvm::Type* DestTy =
3412    cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3413  AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3414                                          ObjCTypes.PtrObjectPtrTy);
3415  llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
3416                                                  AddrWeakObj, "weakread");
3417  read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
3418  return read_weak;
3419}
3420
3421/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3422/// objc_assign_weak (id src, id *dst)
3423///
3424void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
3425                                   llvm::Value *src, llvm::Value *dst) {
3426  llvm::Type * SrcTy = src->getType();
3427  if (!isa<llvm::PointerType>(SrcTy)) {
3428    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3429    assert(Size <= 8 && "does not support size > 8");
3430    src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3431      : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3432    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3433  }
3434  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3435  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3436  CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
3437                          src, dst, "weakassign");
3438  return;
3439}
3440
3441/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3442/// objc_assign_global (id src, id *dst)
3443///
3444void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
3445                                     llvm::Value *src, llvm::Value *dst,
3446                                     bool threadlocal) {
3447  llvm::Type * SrcTy = src->getType();
3448  if (!isa<llvm::PointerType>(SrcTy)) {
3449    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3450    assert(Size <= 8 && "does not support size > 8");
3451    src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3452      : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3453    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3454  }
3455  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3456  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3457  if (!threadlocal)
3458    CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3459                            src, dst, "globalassign");
3460  else
3461    CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3462                            src, dst, "threadlocalassign");
3463  return;
3464}
3465
3466/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
3467/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
3468///
3469void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
3470                                   llvm::Value *src, llvm::Value *dst,
3471                                   llvm::Value *ivarOffset) {
3472  assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
3473  llvm::Type * SrcTy = src->getType();
3474  if (!isa<llvm::PointerType>(SrcTy)) {
3475    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3476    assert(Size <= 8 && "does not support size > 8");
3477    src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3478      : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3479    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3480  }
3481  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3482  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3483  CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3484                          src, dst, ivarOffset);
3485  return;
3486}
3487
3488/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3489/// objc_assign_strongCast (id src, id *dst)
3490///
3491void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
3492                                         llvm::Value *src, llvm::Value *dst) {
3493  llvm::Type * SrcTy = src->getType();
3494  if (!isa<llvm::PointerType>(SrcTy)) {
3495    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3496    assert(Size <= 8 && "does not support size > 8");
3497    src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3498      : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3499    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3500  }
3501  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3502  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3503  CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
3504                          src, dst, "weakassign");
3505  return;
3506}
3507
3508void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
3509                                         llvm::Value *DestPtr,
3510                                         llvm::Value *SrcPtr,
3511                                         llvm::Value *size) {
3512  SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3513  DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
3514  CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
3515                          DestPtr, SrcPtr, size);
3516  return;
3517}
3518
3519/// EmitObjCValueForIvar - Code Gen for ivar reference.
3520///
3521LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3522                                       QualType ObjectTy,
3523                                       llvm::Value *BaseValue,
3524                                       const ObjCIvarDecl *Ivar,
3525                                       unsigned CVRQualifiers) {
3526  const ObjCInterfaceDecl *ID =
3527    ObjectTy->getAs<ObjCObjectType>()->getInterface();
3528  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3529                                  EmitIvarOffset(CGF, ID, Ivar));
3530}
3531
3532llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
3533                                       const ObjCInterfaceDecl *Interface,
3534                                       const ObjCIvarDecl *Ivar) {
3535  uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
3536  return llvm::ConstantInt::get(
3537    CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3538    Offset);
3539}
3540
3541/* *** Private Interface *** */
3542
3543/// EmitImageInfo - Emit the image info marker used to encode some module
3544/// level information.
3545///
3546/// See: <rdr://4810609&4810587&4810587>
3547/// struct IMAGE_INFO {
3548///   unsigned version;
3549///   unsigned flags;
3550/// };
3551enum ImageInfoFlags {
3552  eImageInfo_FixAndContinue      = (1 << 0),
3553  eImageInfo_GarbageCollected    = (1 << 1),
3554  eImageInfo_GCOnly              = (1 << 2),
3555  eImageInfo_OptimizedByDyld     = (1 << 3), // FIXME: When is this set.
3556
3557  // A flag indicating that the module has no instances of a @synthesize of a
3558  // superclass variable. <rdar://problem/6803242>
3559  eImageInfo_CorrectedSynthesize = (1 << 4)
3560};
3561
3562void CGObjCCommonMac::EmitImageInfo() {
3563  unsigned version = 0; // Version is unused?
3564  unsigned flags = 0;
3565
3566  // FIXME: Fix and continue?
3567  if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
3568    flags |= eImageInfo_GarbageCollected;
3569  if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
3570    flags |= eImageInfo_GCOnly;
3571
3572  // We never allow @synthesize of a superclass property.
3573  flags |= eImageInfo_CorrectedSynthesize;
3574
3575  llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3576
3577  // Emitted as int[2];
3578  llvm::Constant *values[2] = {
3579    llvm::ConstantInt::get(Int32Ty, version),
3580    llvm::ConstantInt::get(Int32Ty, flags)
3581  };
3582  llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
3583
3584  const char *Section;
3585  if (ObjCABI == 1)
3586    Section = "__OBJC, __image_info,regular";
3587  else
3588    Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
3589  llvm::GlobalVariable *GV =
3590    CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
3591                      llvm::ConstantArray::get(AT, values),
3592                      Section,
3593                      0,
3594                      true);
3595  GV->setConstant(true);
3596}
3597
3598
3599// struct objc_module {
3600//   unsigned long version;
3601//   unsigned long size;
3602//   const char *name;
3603//   Symtab symtab;
3604// };
3605
3606// FIXME: Get from somewhere
3607static const int ModuleVersion = 7;
3608
3609void CGObjCMac::EmitModuleInfo() {
3610  uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
3611
3612  llvm::Constant *Values[] = {
3613    llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3614    llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3615    // This used to be the filename, now it is unused. <rdr://4327263>
3616    GetClassName(&CGM.getContext().Idents.get("")),
3617    EmitModuleSymbols()
3618  };
3619  CreateMetadataVar("\01L_OBJC_MODULES",
3620                    llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
3621                    "__OBJC,__module_info,regular,no_dead_strip",
3622                    4, true);
3623}
3624
3625llvm::Constant *CGObjCMac::EmitModuleSymbols() {
3626  unsigned NumClasses = DefinedClasses.size();
3627  unsigned NumCategories = DefinedCategories.size();
3628
3629  // Return null if no symbols were defined.
3630  if (!NumClasses && !NumCategories)
3631    return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
3632
3633  llvm::Constant *Values[5];
3634  Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3635  Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
3636  Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3637  Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
3638
3639  // The runtime expects exactly the list of defined classes followed
3640  // by the list of defined categories, in a single array.
3641  std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
3642  for (unsigned i=0; i<NumClasses; i++)
3643    Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3644                                                ObjCTypes.Int8PtrTy);
3645  for (unsigned i=0; i<NumCategories; i++)
3646    Symbols[NumClasses + i] =
3647      llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3648                                     ObjCTypes.Int8PtrTy);
3649
3650  Values[4] =
3651    llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3652                                                  NumClasses + NumCategories),
3653                             Symbols);
3654
3655  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
3656
3657  llvm::GlobalVariable *GV =
3658    CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3659                      "__OBJC,__symbols,regular,no_dead_strip",
3660                      4, true);
3661  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
3662}
3663
3664llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3665                                     IdentifierInfo *II) {
3666  LazySymbols.insert(II);
3667
3668  llvm::GlobalVariable *&Entry = ClassReferences[II];
3669
3670  if (!Entry) {
3671    llvm::Constant *Casted =
3672    llvm::ConstantExpr::getBitCast(GetClassName(II),
3673                                   ObjCTypes.ClassPtrTy);
3674    Entry =
3675    CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3676                      "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3677                      4, true);
3678  }
3679
3680  return Builder.CreateLoad(Entry);
3681}
3682
3683llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3684                                     const ObjCInterfaceDecl *ID) {
3685  return EmitClassRefFromId(Builder, ID->getIdentifier());
3686}
3687
3688llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3689  IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3690  return EmitClassRefFromId(Builder, II);
3691}
3692
3693llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3694                                     bool lvalue) {
3695  llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
3696
3697  if (!Entry) {
3698    llvm::Constant *Casted =
3699      llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
3700                                     ObjCTypes.SelectorPtrTy);
3701    Entry =
3702      CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3703                        "__OBJC,__message_refs,literal_pointers,no_dead_strip",
3704                        4, true);
3705  }
3706
3707  if (lvalue)
3708    return Entry;
3709  return Builder.CreateLoad(Entry);
3710}
3711
3712llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
3713  llvm::GlobalVariable *&Entry = ClassNames[Ident];
3714
3715  if (!Entry)
3716    Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3717                          llvm::ConstantArray::get(VMContext,
3718                                                   Ident->getNameStart()),
3719                              ((ObjCABI == 2) ?
3720                               "__TEXT,__objc_classname,cstring_literals" :
3721                               "__TEXT,__cstring,cstring_literals"),
3722                              1, true);
3723
3724  return getConstantGEP(VMContext, Entry, 0, 0);
3725}
3726
3727llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3728  llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3729      I = MethodDefinitions.find(MD);
3730  if (I != MethodDefinitions.end())
3731    return I->second;
3732
3733  return NULL;
3734}
3735
3736/// GetIvarLayoutName - Returns a unique constant for the given
3737/// ivar layout bitmap.
3738llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
3739                                       const ObjCCommonTypesHelper &ObjCTypes) {
3740  return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3741}
3742
3743void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3744                                                unsigned int BytePos,
3745                                                bool ForStrongLayout,
3746                                                bool &HasUnion) {
3747  const RecordDecl *RD = RT->getDecl();
3748  // FIXME - Use iterator.
3749  SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
3750  llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3751  const llvm::StructLayout *RecLayout =
3752    CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3753
3754  BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3755                      ForStrongLayout, HasUnion);
3756}
3757
3758void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
3759                             const llvm::StructLayout *Layout,
3760                             const RecordDecl *RD,
3761                             const SmallVectorImpl<const FieldDecl*> &RecFields,
3762                             unsigned int BytePos, bool ForStrongLayout,
3763                             bool &HasUnion) {
3764  bool IsUnion = (RD && RD->isUnion());
3765  uint64_t MaxUnionIvarSize = 0;
3766  uint64_t MaxSkippedUnionIvarSize = 0;
3767  const FieldDecl *MaxField = 0;
3768  const FieldDecl *MaxSkippedField = 0;
3769  const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
3770  uint64_t MaxFieldOffset = 0;
3771  uint64_t MaxSkippedFieldOffset = 0;
3772  uint64_t LastBitfieldOrUnnamedOffset = 0;
3773  uint64_t FirstFieldDelta = 0;
3774
3775  if (RecFields.empty())
3776    return;
3777  unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3778  unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
3779  if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
3780    const FieldDecl *FirstField = RecFields[0];
3781    FirstFieldDelta =
3782      ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3783  }
3784
3785  for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
3786    const FieldDecl *Field = RecFields[i];
3787    uint64_t FieldOffset;
3788    if (RD) {
3789      // Note that 'i' here is actually the field index inside RD of Field,
3790      // although this dependency is hidden.
3791      const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3792      FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
3793    } else
3794      FieldOffset =
3795        ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
3796
3797    // Skip over unnamed or bitfields
3798    if (!Field->getIdentifier() || Field->isBitField()) {
3799      LastFieldBitfieldOrUnnamed = Field;
3800      LastBitfieldOrUnnamedOffset = FieldOffset;
3801      continue;
3802    }
3803
3804    LastFieldBitfieldOrUnnamed = 0;
3805    QualType FQT = Field->getType();
3806    if (FQT->isRecordType() || FQT->isUnionType()) {
3807      if (FQT->isUnionType())
3808        HasUnion = true;
3809
3810      BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
3811                                BytePos + FieldOffset,
3812                                ForStrongLayout, HasUnion);
3813      continue;
3814    }
3815
3816    if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3817      const ConstantArrayType *CArray =
3818        dyn_cast_or_null<ConstantArrayType>(Array);
3819      uint64_t ElCount = CArray->getSize().getZExtValue();
3820      assert(CArray && "only array with known element size is supported");
3821      FQT = CArray->getElementType();
3822      while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3823        const ConstantArrayType *CArray =
3824          dyn_cast_or_null<ConstantArrayType>(Array);
3825        ElCount *= CArray->getSize().getZExtValue();
3826        FQT = CArray->getElementType();
3827      }
3828
3829      assert(!FQT->isUnionType() &&
3830             "layout for array of unions not supported");
3831      if (FQT->isRecordType() && ElCount) {
3832        int OldIndex = IvarsInfo.size() - 1;
3833        int OldSkIndex = SkipIvars.size() -1;
3834
3835        const RecordType *RT = FQT->getAs<RecordType>();
3836        BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
3837                                  ForStrongLayout, HasUnion);
3838
3839        // Replicate layout information for each array element. Note that
3840        // one element is already done.
3841        uint64_t ElIx = 1;
3842        for (int FirstIndex = IvarsInfo.size() - 1,
3843               FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
3844          uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
3845          for (int i = OldIndex+1; i <= FirstIndex; ++i)
3846            IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3847                                        IvarsInfo[i].ivar_size));
3848          for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3849            SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3850                                        SkipIvars[i].ivar_size));
3851        }
3852        continue;
3853      }
3854    }
3855    // At this point, we are done with Record/Union and array there of.
3856    // For other arrays we are down to its element type.
3857    Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
3858
3859    unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
3860    if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3861        || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
3862      if (IsUnion) {
3863        uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
3864        if (UnionIvarSize > MaxUnionIvarSize) {
3865          MaxUnionIvarSize = UnionIvarSize;
3866          MaxField = Field;
3867          MaxFieldOffset = FieldOffset;
3868        }
3869      } else {
3870        IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
3871                                    FieldSize / WordSizeInBits));
3872      }
3873    } else if ((ForStrongLayout &&
3874                (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3875               || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
3876      if (IsUnion) {
3877        // FIXME: Why the asymmetry? We divide by word size in bits on other
3878        // side.
3879        uint64_t UnionIvarSize = FieldSize;
3880        if (UnionIvarSize > MaxSkippedUnionIvarSize) {
3881          MaxSkippedUnionIvarSize = UnionIvarSize;
3882          MaxSkippedField = Field;
3883          MaxSkippedFieldOffset = FieldOffset;
3884        }
3885      } else {
3886        // FIXME: Why the asymmetry, we divide by byte size in bits here?
3887        SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
3888                                    FieldSize / ByteSizeInBits));
3889      }
3890    }
3891  }
3892
3893  if (LastFieldBitfieldOrUnnamed) {
3894    if (LastFieldBitfieldOrUnnamed->isBitField()) {
3895      // Last field was a bitfield. Must update skip info.
3896      uint64_t BitFieldSize
3897          = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
3898      GC_IVAR skivar;
3899      skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3900      skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3901        + ((BitFieldSize % ByteSizeInBits) != 0);
3902      SkipIvars.push_back(skivar);
3903    } else {
3904      assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3905      // Last field was unnamed. Must update skip info.
3906      unsigned FieldSize
3907          = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3908      SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3909                                  FieldSize / ByteSizeInBits));
3910    }
3911  }
3912
3913  if (MaxField)
3914    IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
3915                                MaxUnionIvarSize));
3916  if (MaxSkippedField)
3917    SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
3918                                MaxSkippedUnionIvarSize));
3919}
3920
3921/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3922/// the computations and returning the layout bitmap (for ivar or blocks) in
3923/// the given argument BitMap string container. Routine reads
3924/// two containers, IvarsInfo and SkipIvars which are assumed to be
3925/// filled already by the caller.
3926llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
3927  unsigned int WordsToScan, WordsToSkip;
3928  llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3929
3930  // Build the string of skip/scan nibbles
3931  SmallVector<SKIP_SCAN, 32> SkipScanIvars;
3932  unsigned int WordSize =
3933  CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
3934  if (IvarsInfo[0].ivar_bytepos == 0) {
3935    WordsToSkip = 0;
3936    WordsToScan = IvarsInfo[0].ivar_size;
3937  } else {
3938    WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3939    WordsToScan = IvarsInfo[0].ivar_size;
3940  }
3941  for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
3942    unsigned int TailPrevGCObjC =
3943    IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3944    if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
3945      // consecutive 'scanned' object pointers.
3946      WordsToScan += IvarsInfo[i].ivar_size;
3947    } else {
3948      // Skip over 'gc'able object pointer which lay over each other.
3949      if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3950        continue;
3951      // Must skip over 1 or more words. We save current skip/scan values
3952      //  and start a new pair.
3953      SKIP_SCAN SkScan;
3954      SkScan.skip = WordsToSkip;
3955      SkScan.scan = WordsToScan;
3956      SkipScanIvars.push_back(SkScan);
3957
3958      // Skip the hole.
3959      SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3960      SkScan.scan = 0;
3961      SkipScanIvars.push_back(SkScan);
3962      WordsToSkip = 0;
3963      WordsToScan = IvarsInfo[i].ivar_size;
3964    }
3965  }
3966  if (WordsToScan > 0) {
3967    SKIP_SCAN SkScan;
3968    SkScan.skip = WordsToSkip;
3969    SkScan.scan = WordsToScan;
3970    SkipScanIvars.push_back(SkScan);
3971  }
3972
3973  if (!SkipIvars.empty()) {
3974    unsigned int LastIndex = SkipIvars.size()-1;
3975    int LastByteSkipped =
3976    SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3977    LastIndex = IvarsInfo.size()-1;
3978    int LastByteScanned =
3979    IvarsInfo[LastIndex].ivar_bytepos +
3980    IvarsInfo[LastIndex].ivar_size * WordSize;
3981    // Compute number of bytes to skip at the tail end of the last ivar scanned.
3982    if (LastByteSkipped > LastByteScanned) {
3983      unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
3984      SKIP_SCAN SkScan;
3985      SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3986      SkScan.scan = 0;
3987      SkipScanIvars.push_back(SkScan);
3988    }
3989  }
3990  // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3991  // as 0xMN.
3992  int SkipScan = SkipScanIvars.size()-1;
3993  for (int i = 0; i <= SkipScan; i++) {
3994    if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3995        && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3996      // 0xM0 followed by 0x0N detected.
3997      SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3998      for (int j = i+1; j < SkipScan; j++)
3999        SkipScanIvars[j] = SkipScanIvars[j+1];
4000      --SkipScan;
4001    }
4002  }
4003
4004  // Generate the string.
4005  for (int i = 0; i <= SkipScan; i++) {
4006    unsigned char byte;
4007    unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4008    unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4009    unsigned int skip_big  = SkipScanIvars[i].skip / 0xf;
4010    unsigned int scan_big  = SkipScanIvars[i].scan / 0xf;
4011
4012    // first skip big.
4013    for (unsigned int ix = 0; ix < skip_big; ix++)
4014      BitMap += (unsigned char)(0xf0);
4015
4016    // next (skip small, scan)
4017    if (skip_small) {
4018      byte = skip_small << 4;
4019      if (scan_big > 0) {
4020        byte |= 0xf;
4021        --scan_big;
4022      } else if (scan_small) {
4023        byte |= scan_small;
4024        scan_small = 0;
4025      }
4026      BitMap += byte;
4027    }
4028    // next scan big
4029    for (unsigned int ix = 0; ix < scan_big; ix++)
4030      BitMap += (unsigned char)(0x0f);
4031    // last scan small
4032    if (scan_small) {
4033      byte = scan_small;
4034      BitMap += byte;
4035    }
4036  }
4037  // null terminate string.
4038  unsigned char zero = 0;
4039  BitMap += zero;
4040
4041  llvm::GlobalVariable * Entry =
4042  CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
4043                    llvm::ConstantArray::get(VMContext, BitMap.c_str()),
4044                    ((ObjCABI == 2) ?
4045                     "__TEXT,__objc_classname,cstring_literals" :
4046                     "__TEXT,__cstring,cstring_literals"),
4047                    1, true);
4048  return getConstantGEP(VMContext, Entry, 0, 0);
4049}
4050
4051/// BuildIvarLayout - Builds ivar layout bitmap for the class
4052/// implementation for the __strong or __weak case.
4053/// The layout map displays which words in ivar list must be skipped
4054/// and which must be scanned by GC (see below). String is built of bytes.
4055/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4056/// of words to skip and right nibble is count of words to scan. So, each
4057/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4058/// represented by a 0x00 byte which also ends the string.
4059/// 1. when ForStrongLayout is true, following ivars are scanned:
4060/// - id, Class
4061/// - object *
4062/// - __strong anything
4063///
4064/// 2. When ForStrongLayout is false, following ivars are scanned:
4065/// - __weak anything
4066///
4067llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4068  const ObjCImplementationDecl *OMD,
4069  bool ForStrongLayout) {
4070  bool hasUnion = false;
4071
4072  llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
4073  if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
4074      !CGM.getLangOptions().ObjCAutoRefCount)
4075    return llvm::Constant::getNullValue(PtrTy);
4076
4077  const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4078  SmallVector<const FieldDecl*, 32> RecFields;
4079  if (CGM.getLangOptions().ObjCAutoRefCount) {
4080    for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
4081         IVD; IVD = IVD->getNextIvar())
4082      RecFields.push_back(cast<FieldDecl>(IVD));
4083  }
4084  else {
4085    SmallVector<const ObjCIvarDecl*, 32> Ivars;
4086    CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
4087
4088    // FIXME: This is not ideal; we shouldn't have to do this copy.
4089    RecFields.append(Ivars.begin(), Ivars.end());
4090  }
4091
4092  if (RecFields.empty())
4093    return llvm::Constant::getNullValue(PtrTy);
4094
4095  SkipIvars.clear();
4096  IvarsInfo.clear();
4097
4098  BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4099  if (IvarsInfo.empty())
4100    return llvm::Constant::getNullValue(PtrTy);
4101  // Sort on byte position in case we encounterred a union nested in
4102  // the ivar list.
4103  if (hasUnion && !IvarsInfo.empty())
4104    std::sort(IvarsInfo.begin(), IvarsInfo.end());
4105  if (hasUnion && !SkipIvars.empty())
4106    std::sort(SkipIvars.begin(), SkipIvars.end());
4107
4108  std::string BitMap;
4109  llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
4110
4111   if (CGM.getLangOptions().ObjCGCBitmapPrint) {
4112    printf("\n%s ivar layout for class '%s': ",
4113           ForStrongLayout ? "strong" : "weak",
4114           OMD->getClassInterface()->getName().data());
4115    const unsigned char *s = (unsigned char*)BitMap.c_str();
4116    for (unsigned i = 0; i < BitMap.size(); i++)
4117      if (!(s[i] & 0xf0))
4118        printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4119      else
4120        printf("0x%x%s",  s[i], s[i] != 0 ? ", " : "");
4121    printf("\n");
4122  }
4123  return C;
4124}
4125
4126llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
4127  llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4128
4129  // FIXME: Avoid std::string copying.
4130  if (!Entry)
4131    Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
4132                        llvm::ConstantArray::get(VMContext, Sel.getAsString()),
4133                              ((ObjCABI == 2) ?
4134                               "__TEXT,__objc_methname,cstring_literals" :
4135                               "__TEXT,__cstring,cstring_literals"),
4136                              1, true);
4137
4138  return getConstantGEP(VMContext, Entry, 0, 0);
4139}
4140
4141// FIXME: Merge into a single cstring creation function.
4142llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
4143  return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4144}
4145
4146llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
4147  std::string TypeStr;
4148  CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4149
4150  llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4151
4152  if (!Entry)
4153    Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
4154                              llvm::ConstantArray::get(VMContext, TypeStr),
4155                              ((ObjCABI == 2) ?
4156                               "__TEXT,__objc_methtype,cstring_literals" :
4157                               "__TEXT,__cstring,cstring_literals"),
4158                              1, true);
4159
4160  return getConstantGEP(VMContext, Entry, 0, 0);
4161}
4162
4163llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4164                                                  bool Extended) {
4165  std::string TypeStr;
4166  if (CGM.getContext().getObjCEncodingForMethodDecl(
4167                                                const_cast<ObjCMethodDecl*>(D),
4168                                                    TypeStr, Extended))
4169    return 0;
4170
4171  llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4172
4173  if (!Entry)
4174    Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
4175                              llvm::ConstantArray::get(VMContext, TypeStr),
4176                              ((ObjCABI == 2) ?
4177                               "__TEXT,__objc_methtype,cstring_literals" :
4178                               "__TEXT,__cstring,cstring_literals"),
4179                              1, true);
4180
4181  return getConstantGEP(VMContext, Entry, 0, 0);
4182}
4183
4184// FIXME: Merge into a single cstring creation function.
4185llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
4186  llvm::GlobalVariable *&Entry = PropertyNames[Ident];
4187
4188  if (!Entry)
4189    Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
4190                          llvm::ConstantArray::get(VMContext,
4191                                                   Ident->getNameStart()),
4192                              "__TEXT,__cstring,cstring_literals",
4193                              1, true);
4194
4195  return getConstantGEP(VMContext, Entry, 0, 0);
4196}
4197
4198// FIXME: Merge into a single cstring creation function.
4199// FIXME: This Decl should be more precise.
4200llvm::Constant *
4201CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4202                                       const Decl *Container) {
4203  std::string TypeStr;
4204  CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
4205  return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4206}
4207
4208void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
4209                                       const ObjCContainerDecl *CD,
4210                                       SmallVectorImpl<char> &Name) {
4211  llvm::raw_svector_ostream OS(Name);
4212  assert (CD && "Missing container decl in GetNameForMethod");
4213  OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4214     << '[' << CD->getName();
4215  if (const ObjCCategoryImplDecl *CID =
4216      dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
4217    OS << '(' << CID << ')';
4218  OS << ' ' << D->getSelector().getAsString() << ']';
4219}
4220
4221void CGObjCMac::FinishModule() {
4222  EmitModuleInfo();
4223
4224  // Emit the dummy bodies for any protocols which were referenced but
4225  // never defined.
4226  for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
4227         I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4228    if (I->second->hasInitializer())
4229      continue;
4230
4231    llvm::Constant *Values[5];
4232    Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
4233    Values[1] = GetClassName(I->first);
4234    Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
4235    Values[3] = Values[4] =
4236      llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
4237    I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
4238    I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
4239                                                        Values));
4240    CGM.AddUsedGlobal(I->second);
4241  }
4242
4243  // Add assembler directives to add lazy undefined symbol references
4244  // for classes which are referenced but not defined. This is
4245  // important for correct linker interaction.
4246  //
4247  // FIXME: It would be nice if we had an LLVM construct for this.
4248  if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4249    llvm::SmallString<256> Asm;
4250    Asm += CGM.getModule().getModuleInlineAsm();
4251    if (!Asm.empty() && Asm.back() != '\n')
4252      Asm += '\n';
4253
4254    llvm::raw_svector_ostream OS(Asm);
4255    for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4256           e = DefinedSymbols.end(); I != e; ++I)
4257      OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4258         << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
4259    for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
4260         e = LazySymbols.end(); I != e; ++I) {
4261      OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
4262    }
4263
4264    for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4265      OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4266         << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4267    }
4268
4269    CGM.getModule().setModuleInlineAsm(OS.str());
4270  }
4271}
4272
4273CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
4274  : CGObjCCommonMac(cgm),
4275    ObjCTypes(cgm) {
4276  ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
4277  ObjCABI = 2;
4278}
4279
4280/* *** */
4281
4282ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
4283  : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4284{
4285  CodeGen::CodeGenTypes &Types = CGM.getTypes();
4286  ASTContext &Ctx = CGM.getContext();
4287
4288  ShortTy = Types.ConvertType(Ctx.ShortTy);
4289  IntTy = Types.ConvertType(Ctx.IntTy);
4290  LongTy = Types.ConvertType(Ctx.LongTy);
4291  LongLongTy = Types.ConvertType(Ctx.LongLongTy);
4292  Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
4293  Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
4294
4295  ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
4296  PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
4297  SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
4298
4299  // I'm not sure I like this. The implicit coordination is a bit
4300  // gross. We should solve this in a reasonable fashion because this
4301  // is a pretty common task (match some runtime data structure with
4302  // an LLVM data structure).
4303
4304  // FIXME: This is leaked.
4305  // FIXME: Merge with rewriter code?
4306
4307  // struct _objc_super {
4308  //   id self;
4309  //   Class cls;
4310  // }
4311  RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
4312                                      Ctx.getTranslationUnitDecl(),
4313                                      SourceLocation(), SourceLocation(),
4314                                      &Ctx.Idents.get("_objc_super"));
4315  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4316                                Ctx.getObjCIdType(), 0, 0, false, false));
4317  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4318                                Ctx.getObjCClassType(), 0, 0, false, false));
4319  RD->completeDefinition();
4320
4321  SuperCTy = Ctx.getTagDeclType(RD);
4322  SuperPtrCTy = Ctx.getPointerType(SuperCTy);
4323
4324  SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
4325  SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4326
4327  // struct _prop_t {
4328  //   char *name;
4329  //   char *attributes;
4330  // }
4331  PropertyTy = llvm::StructType::create("struct._prop_t",
4332                                        Int8PtrTy, Int8PtrTy, NULL);
4333
4334  // struct _prop_list_t {
4335  //   uint32_t entsize;      // sizeof(struct _prop_t)
4336  //   uint32_t count_of_properties;
4337  //   struct _prop_t prop_list[count_of_properties];
4338  // }
4339  PropertyListTy =
4340    llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4341                             llvm::ArrayType::get(PropertyTy, 0), NULL);
4342  // struct _prop_list_t *
4343  PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
4344
4345  // struct _objc_method {
4346  //   SEL _cmd;
4347  //   char *method_type;
4348  //   char *_imp;
4349  // }
4350  MethodTy = llvm::StructType::create("struct._objc_method",
4351                                      SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4352                                      NULL);
4353
4354  // struct _objc_cache *
4355  CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
4356  CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
4357
4358}
4359
4360ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
4361  : ObjCCommonTypesHelper(cgm) {
4362  // struct _objc_method_description {
4363  //   SEL name;
4364  //   char *types;
4365  // }
4366  MethodDescriptionTy =
4367    llvm::StructType::create("struct._objc_method_description",
4368                             SelectorPtrTy, Int8PtrTy, NULL);
4369
4370  // struct _objc_method_description_list {
4371  //   int count;
4372  //   struct _objc_method_description[1];
4373  // }
4374  MethodDescriptionListTy =
4375    llvm::StructType::create("struct._objc_method_description_list",
4376                             IntTy,
4377                             llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
4378
4379  // struct _objc_method_description_list *
4380  MethodDescriptionListPtrTy =
4381    llvm::PointerType::getUnqual(MethodDescriptionListTy);
4382
4383  // Protocol description structures
4384
4385  // struct _objc_protocol_extension {
4386  //   uint32_t size;  // sizeof(struct _objc_protocol_extension)
4387  //   struct _objc_method_description_list *optional_instance_methods;
4388  //   struct _objc_method_description_list *optional_class_methods;
4389  //   struct _objc_property_list *instance_properties;
4390  //   const char ** extendedMethodTypes;
4391  // }
4392  ProtocolExtensionTy =
4393    llvm::StructType::create("struct._objc_protocol_extension",
4394                             IntTy, MethodDescriptionListPtrTy,
4395                             MethodDescriptionListPtrTy, PropertyListPtrTy,
4396                             Int8PtrPtrTy, NULL);
4397
4398  // struct _objc_protocol_extension *
4399  ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
4400
4401  // Handle recursive construction of Protocol and ProtocolList types
4402
4403  ProtocolTy =
4404    llvm::StructType::create(VMContext, "struct._objc_protocol");
4405
4406  ProtocolListTy =
4407    llvm::StructType::create(VMContext, "struct._objc_protocol_list");
4408  ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
4409                          LongTy,
4410                          llvm::ArrayType::get(ProtocolTy, 0),
4411                          NULL);
4412
4413  // struct _objc_protocol {
4414  //   struct _objc_protocol_extension *isa;
4415  //   char *protocol_name;
4416  //   struct _objc_protocol **_objc_protocol_list;
4417  //   struct _objc_method_description_list *instance_methods;
4418  //   struct _objc_method_description_list *class_methods;
4419  // }
4420  ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4421                      llvm::PointerType::getUnqual(ProtocolListTy),
4422                      MethodDescriptionListPtrTy,
4423                      MethodDescriptionListPtrTy,
4424                      NULL);
4425
4426  // struct _objc_protocol_list *
4427  ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
4428
4429  ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
4430
4431  // Class description structures
4432
4433  // struct _objc_ivar {
4434  //   char *ivar_name;
4435  //   char *ivar_type;
4436  //   int  ivar_offset;
4437  // }
4438  IvarTy = llvm::StructType::create("struct._objc_ivar",
4439                                    Int8PtrTy, Int8PtrTy, IntTy, NULL);
4440
4441  // struct _objc_ivar_list *
4442  IvarListTy =
4443    llvm::StructType::create(VMContext, "struct._objc_ivar_list");
4444  IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
4445
4446  // struct _objc_method_list *
4447  MethodListTy =
4448    llvm::StructType::create(VMContext, "struct._objc_method_list");
4449  MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
4450
4451  // struct _objc_class_extension *
4452  ClassExtensionTy =
4453    llvm::StructType::create("struct._objc_class_extension",
4454                             IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
4455  ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
4456
4457  ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
4458
4459  // struct _objc_class {
4460  //   Class isa;
4461  //   Class super_class;
4462  //   char *name;
4463  //   long version;
4464  //   long info;
4465  //   long instance_size;
4466  //   struct _objc_ivar_list *ivars;
4467  //   struct _objc_method_list *methods;
4468  //   struct _objc_cache *cache;
4469  //   struct _objc_protocol_list *protocols;
4470  //   char *ivar_layout;
4471  //   struct _objc_class_ext *ext;
4472  // };
4473  ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4474                   llvm::PointerType::getUnqual(ClassTy),
4475                   Int8PtrTy,
4476                   LongTy,
4477                   LongTy,
4478                   LongTy,
4479                   IvarListPtrTy,
4480                   MethodListPtrTy,
4481                   CachePtrTy,
4482                   ProtocolListPtrTy,
4483                   Int8PtrTy,
4484                   ClassExtensionPtrTy,
4485                   NULL);
4486
4487  ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
4488
4489  // struct _objc_category {
4490  //   char *category_name;
4491  //   char *class_name;
4492  //   struct _objc_method_list *instance_method;
4493  //   struct _objc_method_list *class_method;
4494  //   uint32_t size;  // sizeof(struct _objc_category)
4495  //   struct _objc_property_list *instance_properties;// category's @property
4496  // }
4497  CategoryTy =
4498    llvm::StructType::create("struct._objc_category",
4499                             Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4500                             MethodListPtrTy, ProtocolListPtrTy,
4501                             IntTy, PropertyListPtrTy, NULL);
4502
4503  // Global metadata structures
4504
4505  // struct _objc_symtab {
4506  //   long sel_ref_cnt;
4507  //   SEL *refs;
4508  //   short cls_def_cnt;
4509  //   short cat_def_cnt;
4510  //   char *defs[cls_def_cnt + cat_def_cnt];
4511  // }
4512  SymtabTy =
4513    llvm::StructType::create("struct._objc_symtab",
4514                             LongTy, SelectorPtrTy, ShortTy, ShortTy,
4515                             llvm::ArrayType::get(Int8PtrTy, 0), NULL);
4516  SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
4517
4518  // struct _objc_module {
4519  //   long version;
4520  //   long size;   // sizeof(struct _objc_module)
4521  //   char *name;
4522  //   struct _objc_symtab* symtab;
4523  //  }
4524  ModuleTy =
4525    llvm::StructType::create("struct._objc_module",
4526                             LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
4527
4528
4529  // FIXME: This is the size of the setjmp buffer and should be target
4530  // specific. 18 is what's used on 32-bit X86.
4531  uint64_t SetJmpBufferSize = 18;
4532
4533  // Exceptions
4534  llvm::Type *StackPtrTy = llvm::ArrayType::get(
4535    llvm::Type::getInt8PtrTy(VMContext), 4);
4536
4537  ExceptionDataTy =
4538    llvm::StructType::create("struct._objc_exception_data",
4539                         llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4540                                              SetJmpBufferSize),
4541                         StackPtrTy, NULL);
4542
4543}
4544
4545ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
4546  : ObjCCommonTypesHelper(cgm) {
4547  // struct _method_list_t {
4548  //   uint32_t entsize;  // sizeof(struct _objc_method)
4549  //   uint32_t method_count;
4550  //   struct _objc_method method_list[method_count];
4551  // }
4552  MethodListnfABITy =
4553    llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4554                             llvm::ArrayType::get(MethodTy, 0), NULL);
4555  // struct method_list_t *
4556  MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
4557
4558  // struct _protocol_t {
4559  //   id isa;  // NULL
4560  //   const char * const protocol_name;
4561  //   const struct _protocol_list_t * protocol_list; // super protocols
4562  //   const struct method_list_t * const instance_methods;
4563  //   const struct method_list_t * const class_methods;
4564  //   const struct method_list_t *optionalInstanceMethods;
4565  //   const struct method_list_t *optionalClassMethods;
4566  //   const struct _prop_list_t * properties;
4567  //   const uint32_t size;  // sizeof(struct _protocol_t)
4568  //   const uint32_t flags;  // = 0
4569  //   const char ** extendedMethodTypes;
4570  // }
4571
4572  // Holder for struct _protocol_list_t *
4573  ProtocolListnfABITy =
4574    llvm::StructType::create(VMContext, "struct._objc_protocol_list");
4575
4576  ProtocolnfABITy =
4577    llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4578                             llvm::PointerType::getUnqual(ProtocolListnfABITy),
4579                             MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4580                             MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4581                             PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4582                             NULL);
4583
4584  // struct _protocol_t*
4585  ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
4586
4587  // struct _protocol_list_t {
4588  //   long protocol_count;   // Note, this is 32/64 bit
4589  //   struct _protocol_t *[protocol_count];
4590  // }
4591  ProtocolListnfABITy->setBody(LongTy,
4592                               llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4593                               NULL);
4594
4595  // struct _objc_protocol_list*
4596  ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
4597
4598  // struct _ivar_t {
4599  //   unsigned long int *offset;  // pointer to ivar offset location
4600  //   char *name;
4601  //   char *type;
4602  //   uint32_t alignment;
4603  //   uint32_t size;
4604  // }
4605  IvarnfABITy =
4606    llvm::StructType::create("struct._ivar_t",
4607                             llvm::PointerType::getUnqual(LongTy),
4608                             Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
4609
4610  // struct _ivar_list_t {
4611  //   uint32 entsize;  // sizeof(struct _ivar_t)
4612  //   uint32 count;
4613  //   struct _iver_t list[count];
4614  // }
4615  IvarListnfABITy =
4616    llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4617                             llvm::ArrayType::get(IvarnfABITy, 0), NULL);
4618
4619  IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
4620
4621  // struct _class_ro_t {
4622  //   uint32_t const flags;
4623  //   uint32_t const instanceStart;
4624  //   uint32_t const instanceSize;
4625  //   uint32_t const reserved;  // only when building for 64bit targets
4626  //   const uint8_t * const ivarLayout;
4627  //   const char *const name;
4628  //   const struct _method_list_t * const baseMethods;
4629  //   const struct _objc_protocol_list *const baseProtocols;
4630  //   const struct _ivar_list_t *const ivars;
4631  //   const uint8_t * const weakIvarLayout;
4632  //   const struct _prop_list_t * const properties;
4633  // }
4634
4635  // FIXME. Add 'reserved' field in 64bit abi mode!
4636  ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4637                                            IntTy, IntTy, IntTy, Int8PtrTy,
4638                                            Int8PtrTy, MethodListnfABIPtrTy,
4639                                            ProtocolListnfABIPtrTy,
4640                                            IvarListnfABIPtrTy,
4641                                            Int8PtrTy, PropertyListPtrTy, NULL);
4642
4643  // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4644  llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
4645  ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4646                 ->getPointerTo();
4647
4648  // struct _class_t {
4649  //   struct _class_t *isa;
4650  //   struct _class_t * const superclass;
4651  //   void *cache;
4652  //   IMP *vtable;
4653  //   struct class_ro_t *ro;
4654  // }
4655
4656  ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
4657  ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4658                        llvm::PointerType::getUnqual(ClassnfABITy),
4659                        CachePtrTy,
4660                        llvm::PointerType::getUnqual(ImpnfABITy),
4661                        llvm::PointerType::getUnqual(ClassRonfABITy),
4662                        NULL);
4663
4664  // LLVM for struct _class_t *
4665  ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
4666
4667  // struct _category_t {
4668  //   const char * const name;
4669  //   struct _class_t *const cls;
4670  //   const struct _method_list_t * const instance_methods;
4671  //   const struct _method_list_t * const class_methods;
4672  //   const struct _protocol_list_t * const protocols;
4673  //   const struct _prop_list_t * const properties;
4674  // }
4675  CategorynfABITy = llvm::StructType::create("struct._category_t",
4676                                             Int8PtrTy, ClassnfABIPtrTy,
4677                                             MethodListnfABIPtrTy,
4678                                             MethodListnfABIPtrTy,
4679                                             ProtocolListnfABIPtrTy,
4680                                             PropertyListPtrTy,
4681                                             NULL);
4682
4683  // New types for nonfragile abi messaging.
4684  CodeGen::CodeGenTypes &Types = CGM.getTypes();
4685  ASTContext &Ctx = CGM.getContext();
4686
4687  // MessageRefTy - LLVM for:
4688  // struct _message_ref_t {
4689  //   IMP messenger;
4690  //   SEL name;
4691  // };
4692
4693  // First the clang type for struct _message_ref_t
4694  RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
4695                                      Ctx.getTranslationUnitDecl(),
4696                                      SourceLocation(), SourceLocation(),
4697                                      &Ctx.Idents.get("_message_ref_t"));
4698  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4699                                Ctx.VoidPtrTy, 0, 0, false, false));
4700  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4701                                Ctx.getObjCSelType(), 0, 0, false, false));
4702  RD->completeDefinition();
4703
4704  MessageRefCTy = Ctx.getTagDeclType(RD);
4705  MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4706  MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
4707
4708  // MessageRefPtrTy - LLVM for struct _message_ref_t*
4709  MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
4710
4711  // SuperMessageRefTy - LLVM for:
4712  // struct _super_message_ref_t {
4713  //   SUPER_IMP messenger;
4714  //   SEL name;
4715  // };
4716  SuperMessageRefTy =
4717    llvm::StructType::create("struct._super_message_ref_t",
4718                             ImpnfABITy, SelectorPtrTy, NULL);
4719
4720  // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4721  SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4722
4723
4724  // struct objc_typeinfo {
4725  //   const void** vtable; // objc_ehtype_vtable + 2
4726  //   const char*  name;    // c++ typeinfo string
4727  //   Class        cls;
4728  // };
4729  EHTypeTy =
4730    llvm::StructType::create("struct._objc_typeinfo",
4731                             llvm::PointerType::getUnqual(Int8PtrTy),
4732                             Int8PtrTy, ClassnfABIPtrTy, NULL);
4733  EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
4734}
4735
4736llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4737  FinishNonFragileABIModule();
4738
4739  return NULL;
4740}
4741
4742void CGObjCNonFragileABIMac::AddModuleClassList(const
4743                                                std::vector<llvm::GlobalValue*>
4744                                                &Container,
4745                                                const char *SymbolName,
4746                                                const char *SectionName) {
4747  unsigned NumClasses = Container.size();
4748
4749  if (!NumClasses)
4750    return;
4751
4752  std::vector<llvm::Constant*> Symbols(NumClasses);
4753  for (unsigned i=0; i<NumClasses; i++)
4754    Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
4755                                                ObjCTypes.Int8PtrTy);
4756  llvm::Constant* Init =
4757    llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4758                                                  NumClasses),
4759                             Symbols);
4760
4761  llvm::GlobalVariable *GV =
4762    new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
4763                             llvm::GlobalValue::InternalLinkage,
4764                             Init,
4765                             SymbolName);
4766  GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
4767  GV->setSection(SectionName);
4768  CGM.AddUsedGlobal(GV);
4769}
4770
4771void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4772  // nonfragile abi has no module definition.
4773
4774  // Build list of all implemented class addresses in array
4775  // L_OBJC_LABEL_CLASS_$.
4776  AddModuleClassList(DefinedClasses,
4777                     "\01L_OBJC_LABEL_CLASS_$",
4778                     "__DATA, __objc_classlist, regular, no_dead_strip");
4779
4780  for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4781    llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4782    if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4783      continue;
4784    IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4785  }
4786
4787  for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4788    llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4789    if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4790      continue;
4791    IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4792  }
4793
4794  AddModuleClassList(DefinedNonLazyClasses,
4795                     "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4796                     "__DATA, __objc_nlclslist, regular, no_dead_strip");
4797
4798  // Build list of all implemented category addresses in array
4799  // L_OBJC_LABEL_CATEGORY_$.
4800  AddModuleClassList(DefinedCategories,
4801                     "\01L_OBJC_LABEL_CATEGORY_$",
4802                     "__DATA, __objc_catlist, regular, no_dead_strip");
4803  AddModuleClassList(DefinedNonLazyCategories,
4804                     "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4805                     "__DATA, __objc_nlcatlist, regular, no_dead_strip");
4806
4807  EmitImageInfo();
4808}
4809
4810/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4811/// VTableDispatchMethods; false otherwise. What this means is that
4812/// except for the 19 selectors in the list, we generate 32bit-style
4813/// message dispatch call for all the rest.
4814bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4815  // At various points we've experimented with using vtable-based
4816  // dispatch for all methods.
4817  switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4818  case CodeGenOptions::Legacy:
4819    return false;
4820  case CodeGenOptions::NonLegacy:
4821    return true;
4822  case CodeGenOptions::Mixed:
4823    break;
4824  }
4825
4826  // If so, see whether this selector is in the white-list of things which must
4827  // use the new dispatch convention. We lazily build a dense set for this.
4828  if (VTableDispatchMethods.empty()) {
4829    VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4830    VTableDispatchMethods.insert(GetNullarySelector("class"));
4831    VTableDispatchMethods.insert(GetNullarySelector("self"));
4832    VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4833    VTableDispatchMethods.insert(GetNullarySelector("length"));
4834    VTableDispatchMethods.insert(GetNullarySelector("count"));
4835
4836    // These are vtable-based if GC is disabled.
4837    // Optimistically use vtable dispatch for hybrid compiles.
4838    if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
4839      VTableDispatchMethods.insert(GetNullarySelector("retain"));
4840      VTableDispatchMethods.insert(GetNullarySelector("release"));
4841      VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4842    }
4843
4844    VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4845    VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4846    VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4847    VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4848    VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4849    VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4850    VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4851
4852    // These are vtable-based if GC is enabled.
4853    // Optimistically use vtable dispatch for hybrid compiles.
4854    if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
4855      VTableDispatchMethods.insert(GetNullarySelector("hash"));
4856      VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4857
4858      // "countByEnumeratingWithState:objects:count"
4859      IdentifierInfo *KeyIdents[] = {
4860        &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4861        &CGM.getContext().Idents.get("objects"),
4862        &CGM.getContext().Idents.get("count")
4863      };
4864      VTableDispatchMethods.insert(
4865        CGM.getContext().Selectors.getSelector(3, KeyIdents));
4866    }
4867  }
4868
4869  return VTableDispatchMethods.count(Sel);
4870}
4871
4872// Metadata flags
4873enum MetaDataDlags {
4874  CLS = 0x0,
4875  CLS_META = 0x1,
4876  CLS_ROOT = 0x2,
4877  OBJC2_CLS_HIDDEN = 0x10,
4878  CLS_EXCEPTION = 0x20,
4879
4880  /// (Obsolete) ARC-specific: this class has a .release_ivars method
4881  CLS_HAS_IVAR_RELEASER = 0x40,
4882  /// class was compiled with -fobjc-arr
4883  CLS_COMPILED_BY_ARC = 0x80  // (1<<7)
4884};
4885/// BuildClassRoTInitializer - generate meta-data for:
4886/// struct _class_ro_t {
4887///   uint32_t const flags;
4888///   uint32_t const instanceStart;
4889///   uint32_t const instanceSize;
4890///   uint32_t const reserved;  // only when building for 64bit targets
4891///   const uint8_t * const ivarLayout;
4892///   const char *const name;
4893///   const struct _method_list_t * const baseMethods;
4894///   const struct _protocol_list_t *const baseProtocols;
4895///   const struct _ivar_list_t *const ivars;
4896///   const uint8_t * const weakIvarLayout;
4897///   const struct _prop_list_t * const properties;
4898/// }
4899///
4900llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4901  unsigned flags,
4902  unsigned InstanceStart,
4903  unsigned InstanceSize,
4904  const ObjCImplementationDecl *ID) {
4905  std::string ClassName = ID->getNameAsString();
4906  llvm::Constant *Values[10]; // 11 for 64bit targets!
4907
4908  if (CGM.getLangOptions().ObjCAutoRefCount)
4909    flags |= CLS_COMPILED_BY_ARC;
4910
4911  Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4912  Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4913  Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4914  // FIXME. For 64bit targets add 0 here.
4915  Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4916    : BuildIvarLayout(ID, true);
4917  Values[ 4] = GetClassName(ID->getIdentifier());
4918  // const struct _method_list_t * const baseMethods;
4919  std::vector<llvm::Constant*> Methods;
4920  std::string MethodListName("\01l_OBJC_$_");
4921  if (flags & CLS_META) {
4922    MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4923    for (ObjCImplementationDecl::classmeth_iterator
4924           i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
4925      // Class methods should always be defined.
4926      Methods.push_back(GetMethodConstant(*i));
4927    }
4928  } else {
4929    MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4930    for (ObjCImplementationDecl::instmeth_iterator
4931           i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
4932      // Instance methods should always be defined.
4933      Methods.push_back(GetMethodConstant(*i));
4934    }
4935    for (ObjCImplementationDecl::propimpl_iterator
4936           i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
4937      ObjCPropertyImplDecl *PID = *i;
4938
4939      if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4940        ObjCPropertyDecl *PD = PID->getPropertyDecl();
4941
4942        if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4943          if (llvm::Constant *C = GetMethodConstant(MD))
4944            Methods.push_back(C);
4945        if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4946          if (llvm::Constant *C = GetMethodConstant(MD))
4947            Methods.push_back(C);
4948      }
4949    }
4950  }
4951  Values[ 5] = EmitMethodList(MethodListName,
4952                              "__DATA, __objc_const", Methods);
4953
4954  const ObjCInterfaceDecl *OID = ID->getClassInterface();
4955  assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4956  Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4957                                + OID->getName(),
4958                                OID->all_referenced_protocol_begin(),
4959                                OID->all_referenced_protocol_end());
4960
4961  if (flags & CLS_META)
4962    Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4963  else
4964    Values[ 7] = EmitIvarList(ID);
4965  Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4966    : BuildIvarLayout(ID, false);
4967  if (flags & CLS_META)
4968    Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4969  else
4970    Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4971                                  ID, ID->getClassInterface(), ObjCTypes);
4972  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4973                                                   Values);
4974  llvm::GlobalVariable *CLASS_RO_GV =
4975    new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4976                             llvm::GlobalValue::InternalLinkage,
4977                             Init,
4978                             (flags & CLS_META) ?
4979                             std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4980                             std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
4981  CLASS_RO_GV->setAlignment(
4982    CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
4983  CLASS_RO_GV->setSection("__DATA, __objc_const");
4984  return CLASS_RO_GV;
4985
4986}
4987
4988/// BuildClassMetaData - This routine defines that to-level meta-data
4989/// for the given ClassName for:
4990/// struct _class_t {
4991///   struct _class_t *isa;
4992///   struct _class_t * const superclass;
4993///   void *cache;
4994///   IMP *vtable;
4995///   struct class_ro_t *ro;
4996/// }
4997///
4998llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4999  std::string &ClassName,
5000  llvm::Constant *IsAGV,
5001  llvm::Constant *SuperClassGV,
5002  llvm::Constant *ClassRoGV,
5003  bool HiddenVisibility) {
5004  llvm::Constant *Values[] = {
5005    IsAGV,
5006    SuperClassGV,
5007    ObjCEmptyCacheVar,  // &ObjCEmptyCacheVar
5008    ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5009    ClassRoGV           // &CLASS_RO_GV
5010  };
5011  if (!Values[1])
5012    Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
5013  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
5014                                                   Values);
5015  llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5016  GV->setInitializer(Init);
5017  GV->setSection("__DATA, __objc_data");
5018  GV->setAlignment(
5019    CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
5020  if (HiddenVisibility)
5021    GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5022  return GV;
5023}
5024
5025bool
5026CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
5027  return OD->getClassMethod(GetNullarySelector("load")) != 0;
5028}
5029
5030void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
5031                                              uint32_t &InstanceStart,
5032                                              uint32_t &InstanceSize) {
5033  const ASTRecordLayout &RL =
5034    CGM.getContext().getASTObjCImplementationLayout(OID);
5035
5036  // InstanceSize is really instance end.
5037  InstanceSize = RL.getDataSize().getQuantity();
5038
5039  // If there are no fields, the start is the same as the end.
5040  if (!RL.getFieldCount())
5041    InstanceStart = InstanceSize;
5042  else
5043    InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
5044}
5045
5046void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5047  std::string ClassName = ID->getNameAsString();
5048  if (!ObjCEmptyCacheVar) {
5049    ObjCEmptyCacheVar = new llvm::GlobalVariable(
5050      CGM.getModule(),
5051      ObjCTypes.CacheTy,
5052      false,
5053      llvm::GlobalValue::ExternalLinkage,
5054      0,
5055      "_objc_empty_cache");
5056
5057    ObjCEmptyVtableVar = new llvm::GlobalVariable(
5058      CGM.getModule(),
5059      ObjCTypes.ImpnfABITy,
5060      false,
5061      llvm::GlobalValue::ExternalLinkage,
5062      0,
5063      "_objc_empty_vtable");
5064  }
5065  assert(ID->getClassInterface() &&
5066         "CGObjCNonFragileABIMac::GenerateClass - class is 0");
5067  // FIXME: Is this correct (that meta class size is never computed)?
5068  uint32_t InstanceStart =
5069    CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
5070  uint32_t InstanceSize = InstanceStart;
5071  uint32_t flags = CLS_META;
5072  std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5073  std::string ObjCClassName(getClassSymbolPrefix());
5074
5075  llvm::GlobalVariable *SuperClassGV, *IsAGV;
5076
5077  bool classIsHidden =
5078    ID->getClassInterface()->getVisibility() == HiddenVisibility;
5079  if (classIsHidden)
5080    flags |= OBJC2_CLS_HIDDEN;
5081  if (ID->hasCXXStructors())
5082    flags |= eClassFlags_ABI2_HasCXXStructors;
5083  if (!ID->getClassInterface()->getSuperClass()) {
5084    // class is root
5085    flags |= CLS_ROOT;
5086    SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
5087    IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
5088  } else {
5089    // Has a root. Current class is not a root.
5090    const ObjCInterfaceDecl *Root = ID->getClassInterface();
5091    while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5092      Root = Super;
5093    IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
5094    if (Root->isWeakImported())
5095      IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5096    // work on super class metadata symbol.
5097    std::string SuperClassName =
5098      ObjCMetaClassName +
5099        ID->getClassInterface()->getSuperClass()->getNameAsString();
5100    SuperClassGV = GetClassGlobal(SuperClassName);
5101    if (ID->getClassInterface()->getSuperClass()->isWeakImported())
5102      SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5103  }
5104  llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5105                                                               InstanceStart,
5106                                                               InstanceSize,ID);
5107  std::string TClassName = ObjCMetaClassName + ClassName;
5108  llvm::GlobalVariable *MetaTClass =
5109    BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5110                       classIsHidden);
5111  DefinedMetaClasses.push_back(MetaTClass);
5112
5113  // Metadata for the class
5114  flags = CLS;
5115  if (classIsHidden)
5116    flags |= OBJC2_CLS_HIDDEN;
5117  if (ID->hasCXXStructors())
5118    flags |= eClassFlags_ABI2_HasCXXStructors;
5119
5120  if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
5121    flags |= CLS_EXCEPTION;
5122
5123  if (!ID->getClassInterface()->getSuperClass()) {
5124    flags |= CLS_ROOT;
5125    SuperClassGV = 0;
5126  } else {
5127    // Has a root. Current class is not a root.
5128    std::string RootClassName =
5129      ID->getClassInterface()->getSuperClass()->getNameAsString();
5130    SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
5131    if (ID->getClassInterface()->getSuperClass()->isWeakImported())
5132      SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5133  }
5134  GetClassSizeInfo(ID, InstanceStart, InstanceSize);
5135  CLASS_RO_GV = BuildClassRoTInitializer(flags,
5136                                         InstanceStart,
5137                                         InstanceSize,
5138                                         ID);
5139
5140  TClassName = ObjCClassName + ClassName;
5141  llvm::GlobalVariable *ClassMD =
5142    BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5143                       classIsHidden);
5144  DefinedClasses.push_back(ClassMD);
5145
5146  // Determine if this class is also "non-lazy".
5147  if (ImplementationIsNonLazy(ID))
5148    DefinedNonLazyClasses.push_back(ClassMD);
5149
5150  // Force the definition of the EHType if necessary.
5151  if (flags & CLS_EXCEPTION)
5152    GetInterfaceEHType(ID->getClassInterface(), true);
5153  // Make sure method definition entries are all clear for next implementation.
5154  MethodDefinitions.clear();
5155}
5156
5157/// GenerateProtocolRef - This routine is called to generate code for
5158/// a protocol reference expression; as in:
5159/// @code
5160///   @protocol(Proto1);
5161/// @endcode
5162/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5163/// which will hold address of the protocol meta-data.
5164///
5165llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
5166                                                         const ObjCProtocolDecl *PD) {
5167
5168  // This routine is called for @protocol only. So, we must build definition
5169  // of protocol's meta-data (not a reference to it!)
5170  //
5171  llvm::Constant *Init =
5172    llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5173                                   ObjCTypes.getExternalProtocolPtrTy());
5174
5175  std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
5176  ProtocolName += PD->getName();
5177
5178  llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5179  if (PTGV)
5180    return Builder.CreateLoad(PTGV);
5181  PTGV = new llvm::GlobalVariable(
5182    CGM.getModule(),
5183    Init->getType(), false,
5184    llvm::GlobalValue::WeakAnyLinkage,
5185    Init,
5186    ProtocolName);
5187  PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5188  PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5189  CGM.AddUsedGlobal(PTGV);
5190  return Builder.CreateLoad(PTGV);
5191}
5192
5193/// GenerateCategory - Build metadata for a category implementation.
5194/// struct _category_t {
5195///   const char * const name;
5196///   struct _class_t *const cls;
5197///   const struct _method_list_t * const instance_methods;
5198///   const struct _method_list_t * const class_methods;
5199///   const struct _protocol_list_t * const protocols;
5200///   const struct _prop_list_t * const properties;
5201/// }
5202///
5203void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
5204  const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
5205  const char *Prefix = "\01l_OBJC_$_CATEGORY_";
5206  std::string ExtCatName(Prefix + Interface->getNameAsString()+
5207                         "_$_" + OCD->getNameAsString());
5208  std::string ExtClassName(getClassSymbolPrefix() +
5209                           Interface->getNameAsString());
5210
5211  llvm::Constant *Values[6];
5212  Values[0] = GetClassName(OCD->getIdentifier());
5213  // meta-class entry symbol
5214  llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
5215  if (Interface->isWeakImported())
5216    ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5217
5218  Values[1] = ClassGV;
5219  std::vector<llvm::Constant*> Methods;
5220  std::string MethodListName(Prefix);
5221  MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
5222    "_$_" + OCD->getNameAsString();
5223
5224  for (ObjCCategoryImplDecl::instmeth_iterator
5225         i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
5226    // Instance methods should always be defined.
5227    Methods.push_back(GetMethodConstant(*i));
5228  }
5229
5230  Values[2] = EmitMethodList(MethodListName,
5231                             "__DATA, __objc_const",
5232                             Methods);
5233
5234  MethodListName = Prefix;
5235  MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5236    OCD->getNameAsString();
5237  Methods.clear();
5238  for (ObjCCategoryImplDecl::classmeth_iterator
5239         i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
5240    // Class methods should always be defined.
5241    Methods.push_back(GetMethodConstant(*i));
5242  }
5243
5244  Values[3] = EmitMethodList(MethodListName,
5245                             "__DATA, __objc_const",
5246                             Methods);
5247  const ObjCCategoryDecl *Category =
5248    Interface->FindCategoryDeclaration(OCD->getIdentifier());
5249  if (Category) {
5250    llvm::SmallString<256> ExtName;
5251    llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5252                                       << OCD->getName();
5253    Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
5254                                 + Interface->getName() + "_$_"
5255                                 + Category->getName(),
5256                                 Category->protocol_begin(),
5257                                 Category->protocol_end());
5258    Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5259                                 OCD, Category, ObjCTypes);
5260  } else {
5261    Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5262    Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
5263  }
5264
5265  llvm::Constant *Init =
5266    llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
5267                              Values);
5268  llvm::GlobalVariable *GCATV
5269    = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
5270                               false,
5271                               llvm::GlobalValue::InternalLinkage,
5272                               Init,
5273                               ExtCatName);
5274  GCATV->setAlignment(
5275    CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
5276  GCATV->setSection("__DATA, __objc_const");
5277  CGM.AddUsedGlobal(GCATV);
5278  DefinedCategories.push_back(GCATV);
5279
5280  // Determine if this category is also "non-lazy".
5281  if (ImplementationIsNonLazy(OCD))
5282    DefinedNonLazyCategories.push_back(GCATV);
5283  // method definition entries must be clear for next implementation.
5284  MethodDefinitions.clear();
5285}
5286
5287/// GetMethodConstant - Return a struct objc_method constant for the
5288/// given method if it has been defined. The result is null if the
5289/// method has not been defined. The return value has type MethodPtrTy.
5290llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
5291  const ObjCMethodDecl *MD) {
5292  llvm::Function *Fn = GetMethodDefinition(MD);
5293  if (!Fn)
5294    return 0;
5295
5296  llvm::Constant *Method[] = {
5297    llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5298                                   ObjCTypes.SelectorPtrTy),
5299    GetMethodVarType(MD),
5300    llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5301  };
5302  return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
5303}
5304
5305/// EmitMethodList - Build meta-data for method declarations
5306/// struct _method_list_t {
5307///   uint32_t entsize;  // sizeof(struct _objc_method)
5308///   uint32_t method_count;
5309///   struct _objc_method method_list[method_count];
5310/// }
5311///
5312llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5313                                                       const char *Section,
5314                                                const ConstantVector &Methods) {
5315  // Return null for empty list.
5316  if (Methods.empty())
5317    return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
5318
5319  llvm::Constant *Values[3];
5320  // sizeof(struct _objc_method)
5321  unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
5322  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5323  // method_count
5324  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
5325  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
5326                                             Methods.size());
5327  Values[2] = llvm::ConstantArray::get(AT, Methods);
5328  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5329
5330  llvm::GlobalVariable *GV =
5331    new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5332                             llvm::GlobalValue::InternalLinkage, Init, Name);
5333  GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
5334  GV->setSection(Section);
5335  CGM.AddUsedGlobal(GV);
5336  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
5337}
5338
5339/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5340/// the given ivar.
5341llvm::GlobalVariable *
5342CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5343                                               const ObjCIvarDecl *Ivar) {
5344  const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
5345  std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
5346    '.' + Ivar->getNameAsString();
5347  llvm::GlobalVariable *IvarOffsetGV =
5348    CGM.getModule().getGlobalVariable(Name);
5349  if (!IvarOffsetGV)
5350    IvarOffsetGV =
5351      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
5352                               false,
5353                               llvm::GlobalValue::ExternalLinkage,
5354                               0,
5355                               Name);
5356  return IvarOffsetGV;
5357}
5358
5359llvm::Constant *
5360CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5361                                          const ObjCIvarDecl *Ivar,
5362                                          unsigned long int Offset) {
5363  llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
5364  IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
5365                                                      Offset));
5366  IvarOffsetGV->setAlignment(
5367    CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
5368
5369  // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5370  // well (i.e., in ObjCIvarOffsetVariable).
5371  if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5372      Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5373      ID->getVisibility() == HiddenVisibility)
5374    IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5375  else
5376    IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
5377  IvarOffsetGV->setSection("__DATA, __objc_ivar");
5378  return IvarOffsetGV;
5379}
5380
5381/// EmitIvarList - Emit the ivar list for the given
5382/// implementation. The return value has type
5383/// IvarListnfABIPtrTy.
5384///  struct _ivar_t {
5385///   unsigned long int *offset;  // pointer to ivar offset location
5386///   char *name;
5387///   char *type;
5388///   uint32_t alignment;
5389///   uint32_t size;
5390/// }
5391/// struct _ivar_list_t {
5392///   uint32 entsize;  // sizeof(struct _ivar_t)
5393///   uint32 count;
5394///   struct _iver_t list[count];
5395/// }
5396///
5397
5398llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
5399  const ObjCImplementationDecl *ID) {
5400
5401  std::vector<llvm::Constant*> Ivars;
5402
5403  const ObjCInterfaceDecl *OID = ID->getClassInterface();
5404  assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
5405
5406  // FIXME. Consolidate this with similar code in GenerateClass.
5407
5408  for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
5409       IVD; IVD = IVD->getNextIvar()) {
5410    // Ignore unnamed bit-fields.
5411    if (!IVD->getDeclName())
5412      continue;
5413    llvm::Constant *Ivar[5];
5414    Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
5415                                ComputeIvarBaseOffset(CGM, ID, IVD));
5416    Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5417    Ivar[2] = GetMethodVarType(IVD);
5418    llvm::Type *FieldTy =
5419      CGM.getTypes().ConvertTypeForMem(IVD->getType());
5420    unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
5421    unsigned Align = CGM.getContext().getPreferredTypeAlign(
5422      IVD->getType().getTypePtr()) >> 3;
5423    Align = llvm::Log2_32(Align);
5424    Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
5425    // NOTE. Size of a bitfield does not match gcc's, because of the
5426    // way bitfields are treated special in each. But I am told that
5427    // 'size' for bitfield ivars is ignored by the runtime so it does
5428    // not matter.  If it matters, there is enough info to get the
5429    // bitfield right!
5430    Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5431    Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
5432  }
5433  // Return null for empty list.
5434  if (Ivars.empty())
5435    return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
5436
5437  llvm::Constant *Values[3];
5438  unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
5439  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5440  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
5441  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
5442                                             Ivars.size());
5443  Values[2] = llvm::ConstantArray::get(AT, Ivars);
5444  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5445  const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5446  llvm::GlobalVariable *GV =
5447    new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5448                             llvm::GlobalValue::InternalLinkage,
5449                             Init,
5450                             Prefix + OID->getName());
5451  GV->setAlignment(
5452    CGM.getTargetData().getABITypeAlignment(Init->getType()));
5453  GV->setSection("__DATA, __objc_const");
5454
5455  CGM.AddUsedGlobal(GV);
5456  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
5457}
5458
5459llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
5460  const ObjCProtocolDecl *PD) {
5461  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
5462
5463  if (!Entry) {
5464    // We use the initializer as a marker of whether this is a forward
5465    // reference or not. At module finalization we add the empty
5466    // contents for protocols which were referenced but never defined.
5467    Entry =
5468      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5469                               llvm::GlobalValue::ExternalLinkage,
5470                               0,
5471                               "\01l_OBJC_PROTOCOL_$_" + PD->getName());
5472    Entry->setSection("__DATA,__datacoal_nt,coalesced");
5473  }
5474
5475  return Entry;
5476}
5477
5478/// GetOrEmitProtocol - Generate the protocol meta-data:
5479/// @code
5480/// struct _protocol_t {
5481///   id isa;  // NULL
5482///   const char * const protocol_name;
5483///   const struct _protocol_list_t * protocol_list; // super protocols
5484///   const struct method_list_t * const instance_methods;
5485///   const struct method_list_t * const class_methods;
5486///   const struct method_list_t *optionalInstanceMethods;
5487///   const struct method_list_t *optionalClassMethods;
5488///   const struct _prop_list_t * properties;
5489///   const uint32_t size;  // sizeof(struct _protocol_t)
5490///   const uint32_t flags;  // = 0
5491///   const char ** extendedMethodTypes;
5492/// }
5493/// @endcode
5494///
5495
5496llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
5497  const ObjCProtocolDecl *PD) {
5498  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
5499
5500  // Early exit if a defining object has already been generated.
5501  if (Entry && Entry->hasInitializer())
5502    return Entry;
5503
5504  // Use the protocol definition, if there is one.
5505  if (const ObjCProtocolDecl *Def = PD->getDefinition())
5506    PD = Def;
5507
5508  // Construct method lists.
5509  std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5510  std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
5511  std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
5512  for (ObjCProtocolDecl::instmeth_iterator
5513         i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
5514    ObjCMethodDecl *MD = *i;
5515    llvm::Constant *C = GetMethodDescriptionConstant(MD);
5516    if (!C)
5517      return GetOrEmitProtocolRef(PD);
5518
5519    if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5520      OptInstanceMethods.push_back(C);
5521      OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
5522    } else {
5523      InstanceMethods.push_back(C);
5524      MethodTypesExt.push_back(GetMethodVarType(MD, true));
5525    }
5526  }
5527
5528  for (ObjCProtocolDecl::classmeth_iterator
5529         i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
5530    ObjCMethodDecl *MD = *i;
5531    llvm::Constant *C = GetMethodDescriptionConstant(MD);
5532    if (!C)
5533      return GetOrEmitProtocolRef(PD);
5534
5535    if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5536      OptClassMethods.push_back(C);
5537      OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
5538    } else {
5539      ClassMethods.push_back(C);
5540      MethodTypesExt.push_back(GetMethodVarType(MD, true));
5541    }
5542  }
5543
5544  MethodTypesExt.insert(MethodTypesExt.end(),
5545                        OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5546
5547  llvm::Constant *Values[11];
5548  // isa is NULL
5549  Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
5550  Values[1] = GetClassName(PD->getIdentifier());
5551  Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5552                               PD->protocol_begin(),
5553                               PD->protocol_end());
5554
5555  Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
5556                             + PD->getName(),
5557                             "__DATA, __objc_const",
5558                             InstanceMethods);
5559  Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
5560                             + PD->getName(),
5561                             "__DATA, __objc_const",
5562                             ClassMethods);
5563  Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
5564                             + PD->getName(),
5565                             "__DATA, __objc_const",
5566                             OptInstanceMethods);
5567  Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
5568                             + PD->getName(),
5569                             "__DATA, __objc_const",
5570                             OptClassMethods);
5571  Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
5572                               0, PD, ObjCTypes);
5573  uint32_t Size =
5574    CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
5575  Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5576  Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
5577  Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5578                                       + PD->getName(),
5579                                       MethodTypesExt, ObjCTypes);
5580  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
5581                                                   Values);
5582
5583  if (Entry) {
5584    // Already created, fix the linkage and update the initializer.
5585    Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
5586    Entry->setInitializer(Init);
5587  } else {
5588    Entry =
5589      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5590                               false, llvm::GlobalValue::WeakAnyLinkage, Init,
5591                               "\01l_OBJC_PROTOCOL_$_" + PD->getName());
5592    Entry->setAlignment(
5593      CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
5594    Entry->setSection("__DATA,__datacoal_nt,coalesced");
5595  }
5596  Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
5597  CGM.AddUsedGlobal(Entry);
5598
5599  // Use this protocol meta-data to build protocol list table in section
5600  // __DATA, __objc_protolist
5601  llvm::GlobalVariable *PTGV =
5602    new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5603                             false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5604                             "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
5605  PTGV->setAlignment(
5606    CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
5607  PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
5608  PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5609  CGM.AddUsedGlobal(PTGV);
5610  return Entry;
5611}
5612
5613/// EmitProtocolList - Generate protocol list meta-data:
5614/// @code
5615/// struct _protocol_list_t {
5616///   long protocol_count;   // Note, this is 32/64 bit
5617///   struct _protocol_t[protocol_count];
5618/// }
5619/// @endcode
5620///
5621llvm::Constant *
5622CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
5623                                      ObjCProtocolDecl::protocol_iterator begin,
5624                                      ObjCProtocolDecl::protocol_iterator end) {
5625  std::vector<llvm::Constant*> ProtocolRefs;
5626
5627  // Just return null for empty protocol lists
5628  if (begin == end)
5629    return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5630
5631  // FIXME: We shouldn't need to do this lookup here, should we?
5632  llvm::SmallString<256> TmpName;
5633  Name.toVector(TmpName);
5634  llvm::GlobalVariable *GV =
5635    CGM.getModule().getGlobalVariable(TmpName.str(), true);
5636  if (GV)
5637    return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
5638
5639  for (; begin != end; ++begin)
5640    ProtocolRefs.push_back(GetProtocolRef(*begin));  // Implemented???
5641
5642  // This list is null terminated.
5643  ProtocolRefs.push_back(llvm::Constant::getNullValue(
5644                           ObjCTypes.ProtocolnfABIPtrTy));
5645
5646  llvm::Constant *Values[2];
5647  Values[0] =
5648    llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
5649  Values[1] =
5650    llvm::ConstantArray::get(
5651      llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5652                           ProtocolRefs.size()),
5653      ProtocolRefs);
5654
5655  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5656  GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5657                                llvm::GlobalValue::InternalLinkage,
5658                                Init, Name);
5659  GV->setSection("__DATA, __objc_const");
5660  GV->setAlignment(
5661    CGM.getTargetData().getABITypeAlignment(Init->getType()));
5662  CGM.AddUsedGlobal(GV);
5663  return llvm::ConstantExpr::getBitCast(GV,
5664                                        ObjCTypes.ProtocolListnfABIPtrTy);
5665}
5666
5667/// GetMethodDescriptionConstant - This routine build following meta-data:
5668/// struct _objc_method {
5669///   SEL _cmd;
5670///   char *method_type;
5671///   char *_imp;
5672/// }
5673
5674llvm::Constant *
5675CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5676  llvm::Constant *Desc[3];
5677  Desc[0] =
5678    llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5679                                   ObjCTypes.SelectorPtrTy);
5680  Desc[1] = GetMethodVarType(MD);
5681  if (!Desc[1])
5682    return 0;
5683
5684  // Protocol methods have no implementation. So, this entry is always NULL.
5685  Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5686  return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
5687}
5688
5689/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5690/// This code gen. amounts to generating code for:
5691/// @code
5692/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5693/// @encode
5694///
5695LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
5696                                               CodeGen::CodeGenFunction &CGF,
5697                                               QualType ObjectTy,
5698                                               llvm::Value *BaseValue,
5699                                               const ObjCIvarDecl *Ivar,
5700                                               unsigned CVRQualifiers) {
5701  ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
5702  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5703                                  EmitIvarOffset(CGF, ID, Ivar));
5704}
5705
5706llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
5707  CodeGen::CodeGenFunction &CGF,
5708  const ObjCInterfaceDecl *Interface,
5709  const ObjCIvarDecl *Ivar) {
5710  return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
5711}
5712
5713static void appendSelectorForMessageRefTable(std::string &buffer,
5714                                             Selector selector) {
5715  if (selector.isUnarySelector()) {
5716    buffer += selector.getNameForSlot(0);
5717    return;
5718  }
5719
5720  for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5721    buffer += selector.getNameForSlot(i);
5722    buffer += '_';
5723  }
5724}
5725
5726/// Emit a "v-table" message send.  We emit a weak hidden-visibility
5727/// struct, initially containing the selector pointer and a pointer to
5728/// a "fixup" variant of the appropriate objc_msgSend.  To call, we
5729/// load and call the function pointer, passing the address of the
5730/// struct as the second parameter.  The runtime determines whether
5731/// the selector is currently emitted using vtable dispatch; if so, it
5732/// substitutes a stub function which simply tail-calls through the
5733/// appropriate vtable slot, and if not, it substitues a stub function
5734/// which tail-calls objc_msgSend.  Both stubs adjust the selector
5735/// argument to correctly point to the selector.
5736RValue
5737CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5738                                              ReturnValueSlot returnSlot,
5739                                              QualType resultType,
5740                                              Selector selector,
5741                                              llvm::Value *arg0,
5742                                              QualType arg0Type,
5743                                              bool isSuper,
5744                                              const CallArgList &formalArgs,
5745                                              const ObjCMethodDecl *method) {
5746  // Compute the actual arguments.
5747  CallArgList args;
5748
5749  // First argument: the receiver / super-call structure.
5750  if (!isSuper)
5751    arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5752  args.add(RValue::get(arg0), arg0Type);
5753
5754  // Second argument: a pointer to the message ref structure.  Leave
5755  // the actual argument value blank for now.
5756  args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5757
5758  args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5759
5760  const CGFunctionInfo &fnInfo =
5761    CGM.getTypes().getFunctionInfo(resultType, args,
5762                                   FunctionType::ExtInfo());
5763
5764  NullReturnState nullReturn;
5765
5766  // Find the function to call and the mangled name for the message
5767  // ref structure.  Using a different mangled name wouldn't actually
5768  // be a problem; it would just be a waste.
5769  //
5770  // The runtime currently never uses vtable dispatch for anything
5771  // except normal, non-super message-sends.
5772  // FIXME: don't use this for that.
5773  llvm::Constant *fn = 0;
5774  std::string messageRefName("\01l_");
5775  if (CGM.ReturnTypeUsesSRet(fnInfo)) {
5776    if (isSuper) {
5777      fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5778      messageRefName += "objc_msgSendSuper2_stret_fixup";
5779    } else {
5780      nullReturn.init(CGF, arg0);
5781      fn = ObjCTypes.getMessageSendStretFixupFn();
5782      messageRefName += "objc_msgSend_stret_fixup";
5783    }
5784  } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5785    fn = ObjCTypes.getMessageSendFpretFixupFn();
5786    messageRefName += "objc_msgSend_fpret_fixup";
5787  } else {
5788    if (isSuper) {
5789      fn = ObjCTypes.getMessageSendSuper2FixupFn();
5790      messageRefName += "objc_msgSendSuper2_fixup";
5791    } else {
5792      fn = ObjCTypes.getMessageSendFixupFn();
5793      messageRefName += "objc_msgSend_fixup";
5794    }
5795  }
5796  assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5797  messageRefName += '_';
5798
5799  // Append the selector name, except use underscores anywhere we
5800  // would have used colons.
5801  appendSelectorForMessageRefTable(messageRefName, selector);
5802
5803  llvm::GlobalVariable *messageRef
5804    = CGM.getModule().getGlobalVariable(messageRefName);
5805  if (!messageRef) {
5806    // Build the message ref structure.
5807    llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
5808    llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
5809    messageRef = new llvm::GlobalVariable(CGM.getModule(),
5810                                          init->getType(),
5811                                          /*constant*/ false,
5812                                          llvm::GlobalValue::WeakAnyLinkage,
5813                                          init,
5814                                          messageRefName);
5815    messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5816    messageRef->setAlignment(16);
5817    messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5818  }
5819  llvm::Value *mref =
5820    CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5821
5822  // Update the message ref argument.
5823  args[1].RV = RValue::get(mref);
5824
5825  // Load the function to call from the message ref table.
5826  llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5827  callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5828
5829  bool variadic = method ? method->isVariadic() : false;
5830  llvm::FunctionType *fnType =
5831    CGF.getTypes().GetFunctionType(fnInfo, variadic);
5832  callee = CGF.Builder.CreateBitCast(callee,
5833                                     llvm::PointerType::getUnqual(fnType));
5834
5835  RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5836  CallArgList CallArgs;
5837  return nullReturn.complete(CGF, result, resultType, CallArgs, 0);
5838}
5839
5840/// Generate code for a message send expression in the nonfragile abi.
5841CodeGen::RValue
5842CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
5843                                            ReturnValueSlot Return,
5844                                            QualType ResultType,
5845                                            Selector Sel,
5846                                            llvm::Value *Receiver,
5847                                            const CallArgList &CallArgs,
5848                                            const ObjCInterfaceDecl *Class,
5849                                            const ObjCMethodDecl *Method) {
5850  return isVTableDispatchedSelector(Sel)
5851    ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
5852                            Receiver, CGF.getContext().getObjCIdType(),
5853                            false, CallArgs, Method)
5854    : EmitMessageSend(CGF, Return, ResultType,
5855                      EmitSelector(CGF.Builder, Sel),
5856                      Receiver, CGF.getContext().getObjCIdType(),
5857                      false, CallArgs, Method, ObjCTypes);
5858}
5859
5860llvm::GlobalVariable *
5861CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
5862  llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5863
5864  if (!GV) {
5865    GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5866                                  false, llvm::GlobalValue::ExternalLinkage,
5867                                  0, Name);
5868  }
5869
5870  return GV;
5871}
5872
5873llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5874                                                        IdentifierInfo *II) {
5875  llvm::GlobalVariable *&Entry = ClassReferences[II];
5876
5877  if (!Entry) {
5878    std::string ClassName(getClassSymbolPrefix() + II->getName().str());
5879    llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5880    Entry =
5881    new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5882                             false, llvm::GlobalValue::InternalLinkage,
5883                             ClassGV,
5884                             "\01L_OBJC_CLASSLIST_REFERENCES_$_");
5885    Entry->setAlignment(
5886                        CGM.getTargetData().getABITypeAlignment(
5887                                                                ObjCTypes.ClassnfABIPtrTy));
5888    Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5889    CGM.AddUsedGlobal(Entry);
5890  }
5891
5892  return Builder.CreateLoad(Entry);
5893}
5894
5895llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5896                                                  const ObjCInterfaceDecl *ID) {
5897  return EmitClassRefFromId(Builder, ID->getIdentifier());
5898}
5899
5900llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5901                                                    CGBuilderTy &Builder) {
5902  IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5903  return EmitClassRefFromId(Builder, II);
5904}
5905
5906llvm::Value *
5907CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5908                                          const ObjCInterfaceDecl *ID) {
5909  llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5910
5911  if (!Entry) {
5912    std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5913    llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5914    Entry =
5915      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5916                               false, llvm::GlobalValue::InternalLinkage,
5917                               ClassGV,
5918                               "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
5919    Entry->setAlignment(
5920      CGM.getTargetData().getABITypeAlignment(
5921        ObjCTypes.ClassnfABIPtrTy));
5922    Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
5923    CGM.AddUsedGlobal(Entry);
5924  }
5925
5926  return Builder.CreateLoad(Entry);
5927}
5928
5929/// EmitMetaClassRef - Return a Value * of the address of _class_t
5930/// meta-data
5931///
5932llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5933                                                      const ObjCInterfaceDecl *ID) {
5934  llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5935  if (Entry)
5936    return Builder.CreateLoad(Entry);
5937
5938  std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
5939  llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
5940  Entry =
5941    new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
5942                             llvm::GlobalValue::InternalLinkage,
5943                             MetaClassGV,
5944                             "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
5945  Entry->setAlignment(
5946    CGM.getTargetData().getABITypeAlignment(
5947      ObjCTypes.ClassnfABIPtrTy));
5948
5949  Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
5950  CGM.AddUsedGlobal(Entry);
5951
5952  return Builder.CreateLoad(Entry);
5953}
5954
5955/// GetClass - Return a reference to the class for the given interface
5956/// decl.
5957llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5958                                              const ObjCInterfaceDecl *ID) {
5959  if (ID->isWeakImported()) {
5960    std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5961    llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5962    ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5963  }
5964
5965  return EmitClassRef(Builder, ID);
5966}
5967
5968/// Generates a message send where the super is the receiver.  This is
5969/// a message send to self with special delivery semantics indicating
5970/// which class's method should be called.
5971CodeGen::RValue
5972CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5973                                                 ReturnValueSlot Return,
5974                                                 QualType ResultType,
5975                                                 Selector Sel,
5976                                                 const ObjCInterfaceDecl *Class,
5977                                                 bool isCategoryImpl,
5978                                                 llvm::Value *Receiver,
5979                                                 bool IsClassMessage,
5980                                                 const CodeGen::CallArgList &CallArgs,
5981                                                 const ObjCMethodDecl *Method) {
5982  // ...
5983  // Create and init a super structure; this is a (receiver, class)
5984  // pair we will pass to objc_msgSendSuper.
5985  llvm::Value *ObjCSuper =
5986    CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
5987
5988  llvm::Value *ReceiverAsObject =
5989    CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5990  CGF.Builder.CreateStore(ReceiverAsObject,
5991                          CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5992
5993  // If this is a class message the metaclass is passed as the target.
5994  llvm::Value *Target;
5995  if (IsClassMessage) {
5996    if (isCategoryImpl) {
5997      // Message sent to "super' in a class method defined in
5998      // a category implementation.
5999      Target = EmitClassRef(CGF.Builder, Class);
6000      Target = CGF.Builder.CreateStructGEP(Target, 0);
6001      Target = CGF.Builder.CreateLoad(Target);
6002    } else
6003      Target = EmitMetaClassRef(CGF.Builder, Class);
6004  } else
6005    Target = EmitSuperClassRef(CGF.Builder, Class);
6006
6007  // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6008  // ObjCTypes types.
6009  llvm::Type *ClassTy =
6010    CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6011  Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6012  CGF.Builder.CreateStore(Target,
6013                          CGF.Builder.CreateStructGEP(ObjCSuper, 1));
6014
6015  return (isVTableDispatchedSelector(Sel))
6016    ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
6017                            ObjCSuper, ObjCTypes.SuperPtrCTy,
6018                            true, CallArgs, Method)
6019    : EmitMessageSend(CGF, Return, ResultType,
6020                      EmitSelector(CGF.Builder, Sel),
6021                      ObjCSuper, ObjCTypes.SuperPtrCTy,
6022                      true, CallArgs, Method, ObjCTypes);
6023}
6024
6025llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
6026                                                  Selector Sel, bool lval) {
6027  llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
6028
6029  if (!Entry) {
6030    llvm::Constant *Casted =
6031      llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6032                                     ObjCTypes.SelectorPtrTy);
6033    Entry =
6034      new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6035                               llvm::GlobalValue::InternalLinkage,
6036                               Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
6037    Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
6038    CGM.AddUsedGlobal(Entry);
6039  }
6040
6041  if (lval)
6042    return Entry;
6043  llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6044
6045  LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6046                  llvm::MDNode::get(VMContext,
6047                                    ArrayRef<llvm::Value*>()));
6048  return LI;
6049}
6050/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
6051/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
6052///
6053void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
6054                                                llvm::Value *src,
6055                                                llvm::Value *dst,
6056                                                llvm::Value *ivarOffset) {
6057  llvm::Type * SrcTy = src->getType();
6058  if (!isa<llvm::PointerType>(SrcTy)) {
6059    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
6060    assert(Size <= 8 && "does not support size > 8");
6061    src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6062           : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
6063    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6064  }
6065  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6066  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
6067  CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6068                          src, dst, ivarOffset);
6069  return;
6070}
6071
6072/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6073/// objc_assign_strongCast (id src, id *dst)
6074///
6075void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
6076  CodeGen::CodeGenFunction &CGF,
6077  llvm::Value *src, llvm::Value *dst) {
6078  llvm::Type * SrcTy = src->getType();
6079  if (!isa<llvm::PointerType>(SrcTy)) {
6080    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
6081    assert(Size <= 8 && "does not support size > 8");
6082    src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6083           : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
6084    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6085  }
6086  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6087  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
6088  CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
6089                          src, dst, "weakassign");
6090  return;
6091}
6092
6093void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
6094  CodeGen::CodeGenFunction &CGF,
6095  llvm::Value *DestPtr,
6096  llvm::Value *SrcPtr,
6097  llvm::Value *Size) {
6098  SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6099  DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
6100  CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
6101                          DestPtr, SrcPtr, Size);
6102  return;
6103}
6104
6105/// EmitObjCWeakRead - Code gen for loading value of a __weak
6106/// object: objc_read_weak (id *src)
6107///
6108llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
6109  CodeGen::CodeGenFunction &CGF,
6110  llvm::Value *AddrWeakObj) {
6111  llvm::Type* DestTy =
6112    cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6113  AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
6114  llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
6115                                                  AddrWeakObj, "weakread");
6116  read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
6117  return read_weak;
6118}
6119
6120/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6121/// objc_assign_weak (id src, id *dst)
6122///
6123void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
6124                                                llvm::Value *src, llvm::Value *dst) {
6125  llvm::Type * SrcTy = src->getType();
6126  if (!isa<llvm::PointerType>(SrcTy)) {
6127    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
6128    assert(Size <= 8 && "does not support size > 8");
6129    src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6130           : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
6131    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6132  }
6133  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6134  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
6135  CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
6136                          src, dst, "weakassign");
6137  return;
6138}
6139
6140/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6141/// objc_assign_global (id src, id *dst)
6142///
6143void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
6144                                          llvm::Value *src, llvm::Value *dst,
6145                                          bool threadlocal) {
6146  llvm::Type * SrcTy = src->getType();
6147  if (!isa<llvm::PointerType>(SrcTy)) {
6148    unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
6149    assert(Size <= 8 && "does not support size > 8");
6150    src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6151           : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
6152    src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6153  }
6154  src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6155  dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
6156  if (!threadlocal)
6157    CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6158                            src, dst, "globalassign");
6159  else
6160    CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6161                            src, dst, "threadlocalassign");
6162  return;
6163}
6164
6165void
6166CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6167                                             const ObjCAtSynchronizedStmt &S) {
6168  EmitAtSynchronizedStmt(CGF, S,
6169      cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6170      cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
6171}
6172
6173llvm::Constant *
6174CGObjCNonFragileABIMac::GetEHType(QualType T) {
6175  // There's a particular fixed type info for 'id'.
6176  if (T->isObjCIdType() ||
6177      T->isObjCQualifiedIdType()) {
6178    llvm::Constant *IDEHType =
6179      CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6180    if (!IDEHType)
6181      IDEHType =
6182        new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6183                                 false,
6184                                 llvm::GlobalValue::ExternalLinkage,
6185                                 0, "OBJC_EHTYPE_id");
6186    return IDEHType;
6187  }
6188
6189  // All other types should be Objective-C interface pointer types.
6190  const ObjCObjectPointerType *PT =
6191    T->getAs<ObjCObjectPointerType>();
6192  assert(PT && "Invalid @catch type.");
6193  const ObjCInterfaceType *IT = PT->getInterfaceType();
6194  assert(IT && "Invalid @catch type.");
6195  return GetInterfaceEHType(IT->getDecl(), false);
6196}
6197
6198void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6199                                         const ObjCAtTryStmt &S) {
6200  EmitTryCatchStmt(CGF, S,
6201      cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6202      cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6203      cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
6204}
6205
6206/// EmitThrowStmt - Generate code for a throw statement.
6207void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6208                                           const ObjCAtThrowStmt &S) {
6209  if (const Expr *ThrowExpr = S.getThrowExpr()) {
6210    llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
6211    Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
6212    CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
6213      .setDoesNotReturn();
6214  } else {
6215    CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
6216      .setDoesNotReturn();
6217  }
6218
6219  CGF.Builder.CreateUnreachable();
6220  CGF.Builder.ClearInsertionPoint();
6221}
6222
6223llvm::Constant *
6224CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
6225                                           bool ForDefinition) {
6226  llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
6227
6228  // If we don't need a definition, return the entry if found or check
6229  // if we use an external reference.
6230  if (!ForDefinition) {
6231    if (Entry)
6232      return Entry;
6233
6234    // If this type (or a super class) has the __objc_exception__
6235    // attribute, emit an external reference.
6236    if (hasObjCExceptionAttribute(CGM.getContext(), ID))
6237      return Entry =
6238        new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
6239                                 llvm::GlobalValue::ExternalLinkage,
6240                                 0,
6241                                 ("OBJC_EHTYPE_$_" +
6242                                  ID->getIdentifier()->getName()));
6243  }
6244
6245  // Otherwise we need to either make a new entry or fill in the
6246  // initializer.
6247  assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
6248  std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6249  std::string VTableName = "objc_ehtype_vtable";
6250  llvm::GlobalVariable *VTableGV =
6251    CGM.getModule().getGlobalVariable(VTableName);
6252  if (!VTableGV)
6253    VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6254                                        false,
6255                                        llvm::GlobalValue::ExternalLinkage,
6256                                        0, VTableName);
6257
6258  llvm::Value *VTableIdx =
6259    llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
6260
6261  llvm::Constant *Values[] = {
6262    llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6263    GetClassName(ID->getIdentifier()),
6264    GetClassGlobal(ClassName)
6265  };
6266  llvm::Constant *Init =
6267    llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
6268
6269  if (Entry) {
6270    Entry->setInitializer(Init);
6271  } else {
6272    Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
6273                                     llvm::GlobalValue::WeakAnyLinkage,
6274                                     Init,
6275                                     ("OBJC_EHTYPE_$_" +
6276                                      ID->getIdentifier()->getName()));
6277  }
6278
6279  if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
6280    Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
6281  Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6282      ObjCTypes.EHTypeTy));
6283
6284  if (ForDefinition) {
6285    Entry->setSection("__DATA,__objc_const");
6286    Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6287  } else {
6288    Entry->setSection("__DATA,__datacoal_nt,coalesced");
6289  }
6290
6291  return Entry;
6292}
6293
6294/* *** */
6295
6296CodeGen::CGObjCRuntime *
6297CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
6298  if (CGM.getLangOptions().ObjCNonFragileABI)
6299    return new CGObjCNonFragileABIMac(CGM);
6300  return new CGObjCMac(CGM);
6301}
6302