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