1 //===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides Objective-C code generation targetting the GNU runtime.  The
11 // class in this file generates structures used by the GNU Objective-C runtime
12 // library.  These structures are defined in objc/objc.h and objc/objc-api.h in
13 // the GNU runtime distribution.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "CGObjCRuntime.h"
18 #include "CodeGenModule.h"
19 #include "CodeGenFunction.h"
20 
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 
27 #include "llvm/Intrinsics.h"
28 #include "llvm/Module.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Target/TargetData.h"
33 
34 #include <map>
35 
36 
37 using namespace clang;
38 using namespace CodeGen;
39 using llvm::dyn_cast;
40 
41 // The version of the runtime that this class targets.  Must match the version
42 // in the runtime.
43 static const int RuntimeVersion = 8;
44 static const int NonFragileRuntimeVersion = 9;
45 static const int ProtocolVersion = 2;
46 
47 namespace {
48 class CGObjCGNU : public CodeGen::CGObjCRuntime {
49 private:
50   CodeGen::CodeGenModule &CGM;
51   llvm::Module &TheModule;
52   const llvm::PointerType *SelectorTy;
53   const llvm::PointerType *PtrToInt8Ty;
54   const llvm::FunctionType *IMPTy;
55   const llvm::PointerType *IdTy;
56   const llvm::IntegerType *IntTy;
57   const llvm::PointerType *PtrTy;
58   const llvm::IntegerType *LongTy;
59   const llvm::PointerType *PtrToIntTy;
60   llvm::GlobalAlias *ClassPtrAlias;
61   llvm::GlobalAlias *MetaClassPtrAlias;
62   std::vector<llvm::Constant*> Classes;
63   std::vector<llvm::Constant*> Categories;
64   std::vector<llvm::Constant*> ConstantStrings;
65   llvm::Function *LoadFunction;
66   llvm::StringMap<llvm::Constant*> ExistingProtocols;
67   typedef std::pair<std::string, std::string> TypedSelector;
68   std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
69   llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
70   // Some zeros used for GEPs in lots of places.
71   llvm::Constant *Zeros[2];
72   llvm::Constant *NULLPtr;
73 private:
74   llvm::Constant *GenerateIvarList(
75       const llvm::SmallVectorImpl<llvm::Constant *>  &IvarNames,
76       const llvm::SmallVectorImpl<llvm::Constant *>  &IvarTypes,
77       const llvm::SmallVectorImpl<llvm::Constant *>  &IvarOffsets);
78   llvm::Constant *GenerateMethodList(const std::string &ClassName,
79       const std::string &CategoryName,
80       const llvm::SmallVectorImpl<Selector>  &MethodSels,
81       const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes,
82       bool isClassMethodList);
83   llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
84   llvm::Constant *GenerateProtocolList(
85       const llvm::SmallVectorImpl<std::string> &Protocols);
86   llvm::Constant *GenerateClassStructure(
87       llvm::Constant *MetaClass,
88       llvm::Constant *SuperClass,
89       unsigned info,
90       const char *Name,
91       llvm::Constant *Version,
92       llvm::Constant *InstanceSize,
93       llvm::Constant *IVars,
94       llvm::Constant *Methods,
95       llvm::Constant *Protocols);
96   llvm::Constant *GenerateProtocolMethodList(
97       const llvm::SmallVectorImpl<llvm::Constant *>  &MethodNames,
98       const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes);
99   llvm::Constant *MakeConstantString(const std::string &Str, const std::string
100       &Name="");
101   llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
102       std::vector<llvm::Constant*> &V, const std::string &Name="");
103   llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
104       std::vector<llvm::Constant*> &V, const std::string &Name="");
105   llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
106       const ObjCIvarDecl *Ivar);
107   void EmitClassRef(const std::string &className);
108 public:
109   CGObjCGNU(CodeGen::CodeGenModule &cgm);
110   virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
111   virtual CodeGen::RValue
112   GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
113                       QualType ResultType,
114                       Selector Sel,
115                       llvm::Value *Receiver,
116                       bool IsClassMessage,
117                       const CallArgList &CallArgs,
118                       const ObjCMethodDecl *Method);
119   virtual CodeGen::RValue
120   GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
121                            QualType ResultType,
122                            Selector Sel,
123                            const ObjCInterfaceDecl *Class,
124                            bool isCategoryImpl,
125                            llvm::Value *Receiver,
126                            bool IsClassMessage,
127                            const CallArgList &CallArgs);
128   virtual llvm::Value *GetClass(CGBuilderTy &Builder,
129                                 const ObjCInterfaceDecl *OID);
130   virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
131   virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
132       *Method);
133 
134   virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
135                                          const ObjCContainerDecl *CD);
136   virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
137   virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
138   virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
139                                            const ObjCProtocolDecl *PD);
140   virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
141   virtual llvm::Function *ModuleInitFunction();
142   virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
143   virtual llvm::Function *GetPropertyGetFunction();
144   virtual llvm::Function *GetPropertySetFunction();
145   virtual llvm::Function *EnumerationMutationFunction();
146 
147   virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
148                                          const Stmt &S);
149   virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
150                              const ObjCAtThrowStmt &S);
151   virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
152                                          llvm::Value *AddrWeakObj);
153   virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
154                                   llvm::Value *src, llvm::Value *dst);
155   virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
156                                     llvm::Value *src, llvm::Value *dest);
157   virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
158                                     llvm::Value *src, llvm::Value *dest);
159   virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
160                                         llvm::Value *src, llvm::Value *dest);
161   virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
162                                       QualType ObjectTy,
163                                       llvm::Value *BaseValue,
164                                       const ObjCIvarDecl *Ivar,
165                                       unsigned CVRQualifiers);
166   virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
167                                       const ObjCInterfaceDecl *Interface,
168                                       const ObjCIvarDecl *Ivar);
169 };
170 } // end anonymous namespace
171 
172 
173 /// Emits a reference to a dummy variable which is emitted with each class.
174 /// This ensures that a linker error will be generated when trying to link
175 /// together modules where a referenced class is not defined.
176 void CGObjCGNU::EmitClassRef(const std::string &className){
177   std::string symbolRef = "__objc_class_ref_" + className;
178   // Don't emit two copies of the same symbol
179   if (TheModule.getGlobalVariable(symbolRef)) return;
180   std::string symbolName = "__objc_class_name_" + className;
181   llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
182   if (!ClassSymbol) {
183     ClassSymbol = new llvm::GlobalVariable(LongTy, false,
184         llvm::GlobalValue::ExternalLinkage, 0, symbolName, &TheModule);
185   }
186   new llvm::GlobalVariable(ClassSymbol->getType(), true,
187     llvm::GlobalValue::CommonLinkage, ClassSymbol, symbolRef,  &TheModule);
188 }
189 
190 static std::string SymbolNameForClass(const std::string &ClassName) {
191   return "_OBJC_CLASS_" + ClassName;
192 }
193 
194 static std::string SymbolNameForMethod(const std::string &ClassName, const
195   std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
196 {
197   return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+
198             (isClassMethod ? "+" : "-") + MethodName;
199 }
200 
201 CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
202   : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
203     MetaClassPtrAlias(0) {
204   IntTy = cast<llvm::IntegerType>(
205       CGM.getTypes().ConvertType(CGM.getContext().IntTy));
206   LongTy = cast<llvm::IntegerType>(
207       CGM.getTypes().ConvertType(CGM.getContext().LongTy));
208 
209   Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
210   Zeros[1] = Zeros[0];
211   NULLPtr = llvm::ConstantPointerNull::get(
212     llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
213   // C string type.  Used in lots of places.
214   PtrToInt8Ty =
215     llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
216   // Get the selector Type.
217   SelectorTy = cast<llvm::PointerType>(
218     CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
219 
220   PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
221   PtrTy = PtrToInt8Ty;
222 
223   // Object type
224   IdTy = cast<llvm::PointerType>(
225 		  CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
226 
227   // IMP type
228   std::vector<const llvm::Type*> IMPArgs;
229   IMPArgs.push_back(IdTy);
230   IMPArgs.push_back(SelectorTy);
231   IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
232 }
233 // This has to perform the lookup every time, since posing and related
234 // techniques can modify the name -> class mapping.
235 llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
236                                  const ObjCInterfaceDecl *OID) {
237   llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
238   EmitClassRef(OID->getNameAsString());
239   ClassName = Builder.CreateStructGEP(ClassName, 0);
240 
241   std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
242   llvm::Constant *ClassLookupFn =
243     CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
244                                                       Params,
245                                                       true),
246                               "objc_lookup_class");
247   return Builder.CreateCall(ClassLookupFn, ClassName);
248 }
249 
250 llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
251   llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
252   if (US == 0)
253     US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
254                                llvm::GlobalValue::InternalLinkage,
255                                ".objc_untyped_selector_alias",
256                                NULL, &TheModule);
257 
258   return Builder.CreateLoad(US);
259 }
260 
261 llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
262     *Method) {
263 
264   std::string SelName = Method->getSelector().getAsString();
265   std::string SelTypes;
266   CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
267   // Typed selectors
268   TypedSelector Selector = TypedSelector(SelName,
269           SelTypes);
270 
271   // If it's already cached, return it.
272   if (TypedSelectors[Selector])
273   {
274       return Builder.CreateLoad(TypedSelectors[Selector]);
275   }
276 
277   // If it isn't, cache it.
278   llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
279           llvm::PointerType::getUnqual(SelectorTy),
280           llvm::GlobalValue::InternalLinkage, SelName,
281           NULL, &TheModule);
282   TypedSelectors[Selector] = Sel;
283 
284   return Builder.CreateLoad(Sel);
285 }
286 
287 llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
288                                               const std::string &Name) {
289   llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
290   ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
291                                llvm::GlobalValue::InternalLinkage,
292                                ConstStr, Name, &TheModule);
293   return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
294 }
295 llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
296     std::vector<llvm::Constant*> &V, const std::string &Name) {
297   llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
298   return new llvm::GlobalVariable(Ty, false,
299       llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
300 }
301 llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
302     std::vector<llvm::Constant*> &V, const std::string &Name) {
303   llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
304   return new llvm::GlobalVariable(Ty, false,
305       llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
306 }
307 
308 /// Generate an NSConstantString object.
309 //TODO: In case there are any crazy people still using the GNU runtime without
310 //an OpenStep implementation, this should let them select their own class for
311 //constant strings.
312 llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
313   std::string Str(SL->getString()->getStrData(),
314                   SL->getString()->getByteLength());
315   std::vector<llvm::Constant*> Ivars;
316   Ivars.push_back(NULLPtr);
317   Ivars.push_back(MakeConstantString(Str));
318   Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
319   llvm::Constant *ObjCStr = MakeGlobal(
320     llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
321     Ivars, ".objc_str");
322   ConstantStrings.push_back(
323       llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
324   return ObjCStr;
325 }
326 
327 ///Generates a message send where the super is the receiver.  This is a message
328 ///send to self with special delivery semantics indicating which class's method
329 ///should be called.
330 CodeGen::RValue
331 CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
332                                     QualType ResultType,
333                                     Selector Sel,
334                                     const ObjCInterfaceDecl *Class,
335                                     bool isCategoryImpl,
336                                     llvm::Value *Receiver,
337                                     bool IsClassMessage,
338                                     const CallArgList &CallArgs) {
339   llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
340 
341   CallArgList ActualArgs;
342 
343   ActualArgs.push_back(
344 	  std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
345 	  CGF.getContext().getObjCIdType()));
346   ActualArgs.push_back(std::make_pair(RValue::get(cmd),
347                                       CGF.getContext().getObjCSelType()));
348   ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
349 
350   CodeGenTypes &Types = CGM.getTypes();
351   const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
352   const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
353 
354   llvm::Value *ReceiverClass = 0;
355   if (isCategoryImpl) {
356     llvm::Constant *classLookupFunction = 0;
357     std::vector<const llvm::Type*> Params;
358     Params.push_back(PtrTy);
359     if (IsClassMessage)  {
360       classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
361             IdTy, Params, true), "objc_get_meta_class");
362     } else {
363       classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
364             IdTy, Params, true), "objc_get_class");
365     }
366     ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
367         MakeConstantString(Class->getNameAsString()));
368   } else {
369     // Set up global aliases for the metaclass or class pointer if they do not
370     // already exist.  These will are forward-references which will be set to
371     // pointers to the class and metaclass structure created for the runtime load
372     // function.  To send a message to super, we look up the value of the
373     // super_class pointer from either the class or metaclass structure.
374     if (IsClassMessage)  {
375       if (!MetaClassPtrAlias) {
376         MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
377             llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
378             Class->getNameAsString(), NULL, &TheModule);
379       }
380       ReceiverClass = MetaClassPtrAlias;
381     } else {
382       if (!ClassPtrAlias) {
383         ClassPtrAlias = new llvm::GlobalAlias(IdTy,
384             llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
385             Class->getNameAsString(), NULL, &TheModule);
386       }
387       ReceiverClass = ClassPtrAlias;
388     }
389   }
390   // Cast the pointer to a simplified version of the class structure
391   ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
392       llvm::PointerType::getUnqual(llvm::StructType::get(IdTy, IdTy, NULL)));
393   // Get the superclass pointer
394   ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
395   // Load the superclass pointer
396   ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
397   // Construct the structure used to look up the IMP
398   llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
399       IdTy, NULL);
400   llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
401 
402   CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
403   CGF.Builder.CreateStore(ReceiverClass,
404       CGF.Builder.CreateStructGEP(ObjCSuper, 1));
405 
406   // Get the IMP
407   std::vector<const llvm::Type*> Params;
408   Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
409   Params.push_back(SelectorTy);
410   llvm::Constant *lookupFunction =
411     CGM.CreateRuntimeFunction(llvm::FunctionType::get(
412           llvm::PointerType::getUnqual(impType), Params, true),
413         "objc_msg_lookup_super");
414 
415   llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
416   llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
417       lookupArgs+2);
418 
419   return CGF.EmitCall(FnInfo, imp, ActualArgs);
420 }
421 
422 /// Generate code for a message send expression.
423 CodeGen::RValue
424 CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
425                                QualType ResultType,
426                                Selector Sel,
427                                llvm::Value *Receiver,
428                                bool IsClassMessage,
429                                const CallArgList &CallArgs,
430                                const ObjCMethodDecl *Method) {
431   llvm::Value *cmd;
432   if (Method)
433     cmd = GetSelector(CGF.Builder, Method);
434   else
435     cmd = GetSelector(CGF.Builder, Sel);
436   CallArgList ActualArgs;
437 
438   ActualArgs.push_back(
439     std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
440     CGF.getContext().getObjCIdType()));
441   ActualArgs.push_back(std::make_pair(RValue::get(cmd),
442                                       CGF.getContext().getObjCSelType()));
443   ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
444 
445   CodeGenTypes &Types = CGM.getTypes();
446   const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
447   const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
448 
449   llvm::Value *imp;
450   std::vector<const llvm::Type*> Params;
451   Params.push_back(Receiver->getType());
452   Params.push_back(SelectorTy);
453   // For sender-aware dispatch, we pass the sender as the third argument to a
454   // lookup function.  When sending messages from C code, the sender is nil.
455   // objc_msg_lookup_sender(id receiver, SEL selector, id sender);
456   if (CGM.getContext().getLangOptions().ObjCSenderDispatch) {
457     llvm::Value *self;
458 
459     if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
460       self = CGF.LoadObjCSelf();
461     } else {
462       self = llvm::ConstantPointerNull::get(IdTy);
463     }
464     Params.push_back(self->getType());
465     llvm::Constant *lookupFunction =
466       CGM.CreateRuntimeFunction(llvm::FunctionType::get(
467           llvm::PointerType::getUnqual(impType), Params, true),
468         "objc_msg_lookup_sender");
469 
470     imp = CGF.Builder.CreateCall3(lookupFunction, Receiver, cmd, self);
471   } else {
472     llvm::Constant *lookupFunction =
473     CGM.CreateRuntimeFunction(llvm::FunctionType::get(
474         llvm::PointerType::getUnqual(impType), Params, true),
475       "objc_msg_lookup");
476 
477     imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
478   }
479 
480   return CGF.EmitCall(FnInfo, imp, ActualArgs);
481 }
482 
483 /// Generates a MethodList.  Used in construction of a objc_class and
484 /// objc_category structures.
485 llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
486                                               const std::string &CategoryName,
487     const llvm::SmallVectorImpl<Selector> &MethodSels,
488     const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
489     bool isClassMethodList) {
490   // Get the method structure type.
491   llvm::StructType *ObjCMethodTy = llvm::StructType::get(
492     PtrToInt8Ty, // Really a selector, but the runtime creates it us.
493     PtrToInt8Ty, // Method types
494     llvm::PointerType::getUnqual(IMPTy), //Method pointer
495     NULL);
496   std::vector<llvm::Constant*> Methods;
497   std::vector<llvm::Constant*> Elements;
498   for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
499     Elements.clear();
500     if (llvm::Constant *Method =
501       TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
502                                                 MethodSels[i].getAsString(),
503                                                 isClassMethodList))) {
504       llvm::Constant *C =
505         CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
506       Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
507       Elements.push_back(
508             llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
509       Method = llvm::ConstantExpr::getBitCast(Method,
510           llvm::PointerType::getUnqual(IMPTy));
511       Elements.push_back(Method);
512       Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
513     }
514   }
515 
516   // Array of method structures
517   llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
518                                                             Methods.size());
519   llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
520                                                          Methods);
521 
522   // Structure containing list pointer, array and array count
523   llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
524   llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
525   llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
526   llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
527       IntTy,
528       ObjCMethodArrayTy,
529       NULL);
530   // Refine next pointer type to concrete type
531   llvm::cast<llvm::OpaqueType>(
532       OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
533   ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
534 
535   Methods.clear();
536   Methods.push_back(llvm::ConstantPointerNull::get(
537         llvm::PointerType::getUnqual(ObjCMethodListTy)));
538   Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
539         MethodTypes.size()));
540   Methods.push_back(MethodArray);
541 
542   // Create an instance of the structure
543   return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
544 }
545 
546 /// Generates an IvarList.  Used in construction of a objc_class.
547 llvm::Constant *CGObjCGNU::GenerateIvarList(
548     const llvm::SmallVectorImpl<llvm::Constant *>  &IvarNames,
549     const llvm::SmallVectorImpl<llvm::Constant *>  &IvarTypes,
550     const llvm::SmallVectorImpl<llvm::Constant *>  &IvarOffsets) {
551   // Get the method structure type.
552   llvm::StructType *ObjCIvarTy = llvm::StructType::get(
553     PtrToInt8Ty,
554     PtrToInt8Ty,
555     IntTy,
556     NULL);
557   std::vector<llvm::Constant*> Ivars;
558   std::vector<llvm::Constant*> Elements;
559   for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
560     Elements.clear();
561     Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
562           Zeros, 2));
563     Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
564           Zeros, 2));
565     Elements.push_back(IvarOffsets[i]);
566     Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
567   }
568 
569   // Array of method structures
570   llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
571       IvarNames.size());
572 
573 
574   Elements.clear();
575   Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
576   Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
577   // Structure containing array and array count
578   llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
579     ObjCIvarArrayTy,
580     NULL);
581 
582   // Create an instance of the structure
583   return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
584 }
585 
586 /// Generate a class structure
587 llvm::Constant *CGObjCGNU::GenerateClassStructure(
588     llvm::Constant *MetaClass,
589     llvm::Constant *SuperClass,
590     unsigned info,
591     const char *Name,
592     llvm::Constant *Version,
593     llvm::Constant *InstanceSize,
594     llvm::Constant *IVars,
595     llvm::Constant *Methods,
596     llvm::Constant *Protocols) {
597   // Set up the class structure
598   // Note:  Several of these are char*s when they should be ids.  This is
599   // because the runtime performs this translation on load.
600   llvm::StructType *ClassTy = llvm::StructType::get(
601       PtrToInt8Ty,        // class_pointer
602       PtrToInt8Ty,        // super_class
603       PtrToInt8Ty,        // name
604       LongTy,             // version
605       LongTy,             // info
606       LongTy,             // instance_size
607       IVars->getType(),   // ivars
608       Methods->getType(), // methods
609       // These are all filled in by the runtime, so we pretend
610       PtrTy,              // dtable
611       PtrTy,              // subclass_list
612       PtrTy,              // sibling_class
613       PtrTy,              // protocols
614       PtrTy,              // gc_object_type
615       NULL);
616   llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
617   llvm::Constant *NullP =
618     llvm::ConstantPointerNull::get(PtrTy);
619   // Fill in the structure
620   std::vector<llvm::Constant*> Elements;
621   Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
622   Elements.push_back(SuperClass);
623   Elements.push_back(MakeConstantString(Name, ".class_name"));
624   Elements.push_back(Zero);
625   Elements.push_back(llvm::ConstantInt::get(LongTy, info));
626   Elements.push_back(InstanceSize);
627   Elements.push_back(IVars);
628   Elements.push_back(Methods);
629   Elements.push_back(NullP);
630   Elements.push_back(NullP);
631   Elements.push_back(NullP);
632   Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
633   Elements.push_back(NullP);
634   // Create an instance of the structure
635   return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
636 }
637 
638 llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
639     const llvm::SmallVectorImpl<llvm::Constant *>  &MethodNames,
640     const llvm::SmallVectorImpl<llvm::Constant *>  &MethodTypes) {
641   // Get the method structure type.
642   llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
643     PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
644     PtrToInt8Ty,
645     NULL);
646   std::vector<llvm::Constant*> Methods;
647   std::vector<llvm::Constant*> Elements;
648   for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
649     Elements.clear();
650     Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
651           Zeros, 2));
652     Elements.push_back(
653           llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
654     Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
655   }
656   llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
657       MethodNames.size());
658   llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
659   llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
660       IntTy, ObjCMethodArrayTy, NULL);
661   Methods.clear();
662   Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
663   Methods.push_back(Array);
664   return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
665 }
666 // Create the protocol list structure used in classes, categories and so on
667 llvm::Constant *CGObjCGNU::GenerateProtocolList(
668     const llvm::SmallVectorImpl<std::string> &Protocols) {
669   llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
670       Protocols.size());
671   llvm::StructType *ProtocolListTy = llvm::StructType::get(
672       PtrTy, //Should be a recurisve pointer, but it's always NULL here.
673       LongTy,//FIXME: Should be size_t
674       ProtocolArrayTy,
675       NULL);
676   std::vector<llvm::Constant*> Elements;
677   for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
678       iter != endIter ; iter++) {
679     llvm::Constant *protocol = ExistingProtocols[*iter];
680     if (!protocol)
681       protocol = GenerateEmptyProtocol(*iter);
682     llvm::Constant *Ptr =
683       llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
684     Elements.push_back(Ptr);
685   }
686   llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
687       Elements);
688   Elements.clear();
689   Elements.push_back(NULLPtr);
690   Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
691   Elements.push_back(ProtocolArray);
692   return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
693 }
694 
695 llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
696                                             const ObjCProtocolDecl *PD) {
697   llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
698   const llvm::Type *T =
699     CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
700   return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
701 }
702 
703 llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
704   const std::string &ProtocolName) {
705   llvm::SmallVector<std::string, 0> EmptyStringVector;
706   llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
707 
708   llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
709   llvm::Constant *InstanceMethodList =
710     GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
711   llvm::Constant *ClassMethodList =
712     GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
713   // Protocols are objects containing lists of the methods implemented and
714   // protocols adopted.
715   llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
716       PtrToInt8Ty,
717       ProtocolList->getType(),
718       InstanceMethodList->getType(),
719       ClassMethodList->getType(),
720       NULL);
721   std::vector<llvm::Constant*> Elements;
722   // The isa pointer must be set to a magic number so the runtime knows it's
723   // the correct layout.
724   Elements.push_back(llvm::ConstantExpr::getIntToPtr(
725         llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
726   Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
727   Elements.push_back(ProtocolList);
728   Elements.push_back(InstanceMethodList);
729   Elements.push_back(ClassMethodList);
730   return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
731 }
732 
733 void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
734   ASTContext &Context = CGM.getContext();
735   std::string ProtocolName = PD->getNameAsString();
736   llvm::SmallVector<std::string, 16> Protocols;
737   for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
738        E = PD->protocol_end(); PI != E; ++PI)
739     Protocols.push_back((*PI)->getNameAsString());
740   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
741   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
742   for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
743        E = PD->instmeth_end(); iter != E; iter++) {
744     std::string TypeStr;
745     Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
746     InstanceMethodNames.push_back(
747         CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
748     InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
749   }
750   // Collect information about class methods:
751   llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
752   llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
753   for (ObjCProtocolDecl::classmeth_iterator
754          iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
755        iter != endIter ; iter++) {
756     std::string TypeStr;
757     Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
758     ClassMethodNames.push_back(
759         CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
760     ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
761   }
762 
763   llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
764   llvm::Constant *InstanceMethodList =
765     GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
766   llvm::Constant *ClassMethodList =
767     GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
768   // Protocols are objects containing lists of the methods implemented and
769   // protocols adopted.
770   llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
771       PtrToInt8Ty,
772       ProtocolList->getType(),
773       InstanceMethodList->getType(),
774       ClassMethodList->getType(),
775       NULL);
776   std::vector<llvm::Constant*> Elements;
777   // The isa pointer must be set to a magic number so the runtime knows it's
778   // the correct layout.
779   Elements.push_back(llvm::ConstantExpr::getIntToPtr(
780         llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
781   Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
782   Elements.push_back(ProtocolList);
783   Elements.push_back(InstanceMethodList);
784   Elements.push_back(ClassMethodList);
785   ExistingProtocols[ProtocolName] =
786     llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
787           ".objc_protocol"), IdTy);
788 }
789 
790 void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
791   std::string ClassName = OCD->getClassInterface()->getNameAsString();
792   std::string CategoryName = OCD->getNameAsString();
793   // Collect information about instance methods
794   llvm::SmallVector<Selector, 16> InstanceMethodSels;
795   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
796   for (ObjCCategoryImplDecl::instmeth_iterator
797          iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
798        iter != endIter ; iter++) {
799     InstanceMethodSels.push_back((*iter)->getSelector());
800     std::string TypeStr;
801     CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
802     InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
803   }
804 
805   // Collect information about class methods
806   llvm::SmallVector<Selector, 16> ClassMethodSels;
807   llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
808   for (ObjCCategoryImplDecl::classmeth_iterator
809          iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
810        iter != endIter ; iter++) {
811     ClassMethodSels.push_back((*iter)->getSelector());
812     std::string TypeStr;
813     CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
814     ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
815   }
816 
817   // Collect the names of referenced protocols
818   llvm::SmallVector<std::string, 16> Protocols;
819   const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
820   const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
821   for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
822        E = Protos.end(); I != E; ++I)
823     Protocols.push_back((*I)->getNameAsString());
824 
825   std::vector<llvm::Constant*> Elements;
826   Elements.push_back(MakeConstantString(CategoryName));
827   Elements.push_back(MakeConstantString(ClassName));
828   // Instance method list
829   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
830           ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
831           false), PtrTy));
832   // Class method list
833   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
834           ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
835         PtrTy));
836   // Protocol list
837   Elements.push_back(llvm::ConstantExpr::getBitCast(
838         GenerateProtocolList(Protocols), PtrTy));
839   Categories.push_back(llvm::ConstantExpr::getBitCast(
840         MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
841             PtrTy, PtrTy, NULL), Elements), PtrTy));
842 }
843 
844 void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
845   ASTContext &Context = CGM.getContext();
846 
847   // Get the superclass name.
848   const ObjCInterfaceDecl * SuperClassDecl =
849     OID->getClassInterface()->getSuperClass();
850   std::string SuperClassName;
851   if (SuperClassDecl) {
852     SuperClassName = SuperClassDecl->getNameAsString();
853     EmitClassRef(SuperClassName);
854   }
855 
856   // Get the class name
857   ObjCInterfaceDecl *ClassDecl =
858     const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
859   std::string ClassName = ClassDecl->getNameAsString();
860   // Emit the symbol that is used to generate linker errors if this class is
861   // referenced in other modules but not declared.
862   std::string classSymbolName = "__objc_class_name_" + ClassName;
863   if (llvm::GlobalVariable *symbol =
864       TheModule.getGlobalVariable(classSymbolName)) {
865     symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
866   } else {
867     new llvm::GlobalVariable(LongTy, false, llvm::GlobalValue::ExternalLinkage,
868     llvm::ConstantInt::get(LongTy, 0), classSymbolName, &TheModule);
869   }
870 
871   // Get the size of instances.
872   int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
873 
874   // Collect information about instance variables.
875   llvm::SmallVector<llvm::Constant*, 16> IvarNames;
876   llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
877   llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
878 
879   int superInstanceSize = !SuperClassDecl ? 0 :
880     Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
881   // For non-fragile ivars, set the instance size to 0 - {the size of just this
882   // class}.  The runtime will then set this to the correct value on load.
883   if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
884     instanceSize = 0 - (instanceSize - superInstanceSize);
885   }
886   for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
887       endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
888       // Store the name
889       IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
890                                                          ->getNameAsString()));
891       // Get the type encoding for this ivar
892       std::string TypeStr;
893       Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
894       IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
895       // Get the offset
896       uint64_t Offset;
897       if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
898 		Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter) -
899 			superInstanceSize;
900         ObjCIvarOffsetVariable(ClassDecl, *iter);
901       } else {
902         Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
903       }
904       IvarOffsets.push_back(
905           llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset));
906   }
907 
908   // Collect information about instance methods
909   llvm::SmallVector<Selector, 16> InstanceMethodSels;
910   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
911   for (ObjCImplementationDecl::instmeth_iterator
912          iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
913        iter != endIter ; iter++) {
914     InstanceMethodSels.push_back((*iter)->getSelector());
915     std::string TypeStr;
916     Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
917     InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
918   }
919   for (ObjCImplDecl::propimpl_iterator
920          iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
921        iter != endIter ; iter++) {
922     ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
923     if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
924       InstanceMethodSels.push_back(getter->getSelector());
925       std::string TypeStr;
926       Context.getObjCEncodingForMethodDecl(getter,TypeStr);
927       InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
928     }
929     if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
930       InstanceMethodSels.push_back(setter->getSelector());
931       std::string TypeStr;
932       Context.getObjCEncodingForMethodDecl(setter,TypeStr);
933       InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
934     }
935   }
936 
937   // Collect information about class methods
938   llvm::SmallVector<Selector, 16> ClassMethodSels;
939   llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
940   for (ObjCImplementationDecl::classmeth_iterator
941          iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
942        iter != endIter ; iter++) {
943     ClassMethodSels.push_back((*iter)->getSelector());
944     std::string TypeStr;
945     Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
946     ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
947   }
948   // Collect the names of referenced protocols
949   llvm::SmallVector<std::string, 16> Protocols;
950   const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
951   for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
952        E = Protos.end(); I != E; ++I)
953     Protocols.push_back((*I)->getNameAsString());
954 
955 
956 
957   // Get the superclass pointer.
958   llvm::Constant *SuperClass;
959   if (!SuperClassName.empty()) {
960     SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
961   } else {
962     SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
963   }
964   // Empty vector used to construct empty method lists
965   llvm::SmallVector<llvm::Constant*, 1>  empty;
966   // Generate the method and instance variable lists
967   llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
968       InstanceMethodSels, InstanceMethodTypes, false);
969   llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
970       ClassMethodSels, ClassMethodTypes, true);
971   llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
972       IvarOffsets);
973   //Generate metaclass for class methods
974   llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
975       NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
976         empty, empty, empty), ClassMethodList, NULLPtr);
977 
978   // Generate the class structure
979   llvm::Constant *ClassStruct =
980     GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
981                            ClassName.c_str(), 0,
982       llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
983       MethodList, GenerateProtocolList(Protocols));
984 
985   // Resolve the class aliases, if they exist.
986   if (ClassPtrAlias) {
987     ClassPtrAlias->setAliasee(
988         llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
989     ClassPtrAlias = 0;
990   }
991   if (MetaClassPtrAlias) {
992     MetaClassPtrAlias->setAliasee(
993         llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
994     MetaClassPtrAlias = 0;
995   }
996 
997   // Add class structure to list to be added to the symtab later
998   ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
999   Classes.push_back(ClassStruct);
1000 }
1001 
1002 void CGObjCGNU::MergeMetadataGlobals(
1003                           std::vector<llvm::Constant*> &UsedArray) {
1004 }
1005 
1006 llvm::Function *CGObjCGNU::ModuleInitFunction() {
1007   // Only emit an ObjC load function if no Objective-C stuff has been called
1008   if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1009       ExistingProtocols.empty() && TypedSelectors.empty() &&
1010       UntypedSelectors.empty())
1011     return NULL;
1012 
1013   const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1014           SelectorTy->getElementType());
1015   const llvm::Type *SelStructPtrTy = SelectorTy;
1016   bool isSelOpaque = false;
1017   if (SelStructTy == 0) {
1018     SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
1019     SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
1020     isSelOpaque = true;
1021   }
1022 
1023   // Name the ObjC types to make the IR a bit easier to read
1024   TheModule.addTypeName(".objc_selector", SelStructPtrTy);
1025   TheModule.addTypeName(".objc_id", IdTy);
1026   TheModule.addTypeName(".objc_imp", IMPTy);
1027 
1028   std::vector<llvm::Constant*> Elements;
1029   llvm::Constant *Statics = NULLPtr;
1030   // Generate statics list:
1031   if (ConstantStrings.size()) {
1032     llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
1033         ConstantStrings.size() + 1);
1034     ConstantStrings.push_back(NULLPtr);
1035     Elements.push_back(MakeConstantString("NSConstantString",
1036           ".objc_static_class_name"));
1037     Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
1038        ConstantStrings));
1039     llvm::StructType *StaticsListTy =
1040       llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
1041     llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
1042     Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
1043     llvm::ArrayType *StaticsListArrayTy =
1044       llvm::ArrayType::get(StaticsListPtrTy, 2);
1045     Elements.clear();
1046     Elements.push_back(Statics);
1047     Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
1048     Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
1049     Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
1050   }
1051   // Array of classes, categories, and constant objects
1052   llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
1053       Classes.size() + Categories.size()  + 2);
1054   llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
1055                                                      llvm::Type::Int16Ty,
1056                                                      llvm::Type::Int16Ty,
1057                                                      ClassListTy, NULL);
1058 
1059   Elements.clear();
1060   // Pointer to an array of selectors used in this module.
1061   std::vector<llvm::Constant*> Selectors;
1062   for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1063      iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1064      iter != iterEnd ; ++iter) {
1065     Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
1066     Elements.push_back(MakeConstantString(iter->first.second,
1067                                           ".objc_sel_types"));
1068     Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
1069     Elements.clear();
1070   }
1071   for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1072       iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1073       iter != iterEnd; ++iter) {
1074     Elements.push_back(
1075         MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
1076     Elements.push_back(NULLPtr);
1077     Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
1078     Elements.clear();
1079   }
1080   Elements.push_back(NULLPtr);
1081   Elements.push_back(NULLPtr);
1082   Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
1083   Elements.clear();
1084   // Number of static selectors
1085   Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
1086   llvm::Constant *SelectorList = MakeGlobal(
1087           llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
1088           ".objc_selector_list");
1089   Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
1090     SelStructPtrTy));
1091 
1092   // Now that all of the static selectors exist, create pointers to them.
1093   int index = 0;
1094   for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1095      iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1096      iter != iterEnd; ++iter) {
1097     llvm::Constant *Idxs[] = {Zeros[0],
1098       llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
1099     llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
1100         true, llvm::GlobalValue::InternalLinkage,
1101         llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1102         ".objc_sel_ptr", &TheModule);
1103     // If selectors are defined as an opaque type, cast the pointer to this
1104     // type.
1105     if (isSelOpaque) {
1106       SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
1107         llvm::PointerType::getUnqual(SelectorTy));
1108     }
1109     (*iter).second->setAliasee(SelPtr);
1110   }
1111   for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1112       iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1113       iter != iterEnd; iter++) {
1114     llvm::Constant *Idxs[] = {Zeros[0],
1115       llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
1116     llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
1117         llvm::GlobalValue::InternalLinkage,
1118         llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1119         ".objc_sel_ptr", &TheModule);
1120     // If selectors are defined as an opaque type, cast the pointer to this
1121     // type.
1122     if (isSelOpaque) {
1123       SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
1124         llvm::PointerType::getUnqual(SelectorTy));
1125     }
1126     (*iter).second->setAliasee(SelPtr);
1127   }
1128   // Number of classes defined.
1129   Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
1130         Classes.size()));
1131   // Number of categories defined
1132   Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
1133         Categories.size()));
1134   // Create an array of classes, then categories, then static object instances
1135   Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1136   //  NULL-terminated list of static object instances (mainly constant strings)
1137   Classes.push_back(Statics);
1138   Classes.push_back(NULLPtr);
1139   llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
1140   Elements.push_back(ClassList);
1141   // Construct the symbol table
1142   llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1143 
1144   // The symbol table is contained in a module which has some version-checking
1145   // constants
1146   llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
1147       PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
1148   Elements.clear();
1149   // Runtime version used for compatibility checking.
1150   if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1151 	Elements.push_back(llvm::ConstantInt::get(LongTy,
1152         NonFragileRuntimeVersion));
1153   } else {
1154     Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
1155   }
1156   // sizeof(ModuleTy)
1157   llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1158   Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
1159   //FIXME: Should be the path to the file where this module was declared
1160   Elements.push_back(NULLPtr);
1161   Elements.push_back(SymTab);
1162   llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1163 
1164   // Create the load function calling the runtime entry point with the module
1165   // structure
1166   llvm::Function * LoadFunction = llvm::Function::Create(
1167       llvm::FunctionType::get(llvm::Type::VoidTy, false),
1168       llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1169       &TheModule);
1170   llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
1171   CGBuilderTy Builder;
1172   Builder.SetInsertPoint(EntryBB);
1173 
1174   std::vector<const llvm::Type*> Params(1,
1175       llvm::PointerType::getUnqual(ModuleTy));
1176   llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1177         llvm::Type::VoidTy, Params, true), "__objc_exec_class");
1178   Builder.CreateCall(Register, Module);
1179   Builder.CreateRetVoid();
1180 
1181   return LoadFunction;
1182 }
1183 
1184 llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1185                                           const ObjCContainerDecl *CD) {
1186   const ObjCCategoryImplDecl *OCD =
1187     dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
1188   std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1189   std::string ClassName = OMD->getClassInterface()->getNameAsString();
1190   std::string MethodName = OMD->getSelector().getAsString();
1191   bool isClassMethod = !OMD->isInstanceMethod();
1192 
1193   CodeGenTypes &Types = CGM.getTypes();
1194   const llvm::FunctionType *MethodTy =
1195     Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
1196   std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1197       MethodName, isClassMethod);
1198 
1199   llvm::Function *Method = llvm::Function::Create(MethodTy,
1200       llvm::GlobalValue::InternalLinkage,
1201       FunctionName,
1202       &TheModule);
1203   return Method;
1204 }
1205 
1206 llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1207 	std::vector<const llvm::Type*> Params;
1208 	const llvm::Type *BoolTy =
1209 		CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1210 	Params.push_back(IdTy);
1211 	Params.push_back(SelectorTy);
1212 	// FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1213 	Params.push_back(LongTy);
1214 	Params.push_back(BoolTy);
1215 	// void objc_getProperty (id, SEL, ptrdiff_t, bool)
1216 	const llvm::FunctionType *FTy =
1217 		llvm::FunctionType::get(IdTy, Params, false);
1218 	return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1219 				"objc_getProperty"));
1220 }
1221 
1222 llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1223 	std::vector<const llvm::Type*> Params;
1224 	const llvm::Type *BoolTy =
1225 		CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1226 	Params.push_back(IdTy);
1227 	Params.push_back(SelectorTy);
1228 	// FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1229 	Params.push_back(LongTy);
1230 	Params.push_back(IdTy);
1231 	Params.push_back(BoolTy);
1232 	Params.push_back(BoolTy);
1233 	// void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1234 	const llvm::FunctionType *FTy =
1235 		llvm::FunctionType::get(llvm::Type::VoidTy, Params, false);
1236 	return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1237 				"objc_setProperty"));
1238 }
1239 
1240 llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
1241   std::vector<const llvm::Type*> Params(1, IdTy);
1242   return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1243         llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1244         "objc_enumerationMutation"));
1245 }
1246 
1247 void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1248                                           const Stmt &S) {
1249   // Pointer to the personality function
1250   llvm::Constant *Personality =
1251     CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
1252           true),
1253         "__gnu_objc_personality_v0");
1254   Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
1255   std::vector<const llvm::Type*> Params;
1256   Params.push_back(PtrTy);
1257   llvm::Value *RethrowFn =
1258     CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
1259           Params, false), "_Unwind_Resume_or_Rethrow");
1260 
1261   bool isTry = isa<ObjCAtTryStmt>(S);
1262   llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1263   llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1264   llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
1265   llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
1266   llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1267   llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1268   llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1269 
1270   // GNU runtime does not currently support @synchronized()
1271   if (!isTry) {
1272     std::vector<const llvm::Type*> Args(1, IdTy);
1273     llvm::FunctionType *FTy =
1274       llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
1275     llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
1276     llvm::Value *SyncArg =
1277       CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1278     SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1279     CGF.Builder.CreateCall(SyncEnter, SyncArg);
1280   }
1281 
1282 
1283   // Push an EH context entry, used for handling rethrows and jumps
1284   // through finally.
1285   CGF.PushCleanupBlock(FinallyBlock);
1286 
1287   // Emit the statements in the @try {} block
1288   CGF.setInvokeDest(TryHandler);
1289 
1290   CGF.EmitBlock(TryBlock);
1291   CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1292                      : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1293 
1294   // Jump to @finally if there is no exception
1295   CGF.EmitBranchThroughCleanup(FinallyEnd);
1296 
1297   // Emit the handlers
1298   CGF.EmitBlock(TryHandler);
1299 
1300   // Get the correct versions of the exception handling intrinsics
1301   llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1302   int PointerWidth = td.getTypeSizeInBits(PtrTy);
1303   assert((PointerWidth == 32 || PointerWidth == 64) &&
1304     "Can't yet handle exceptions if pointers are not 32 or 64 bits");
1305   llvm::Value *llvm_eh_exception =
1306     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
1307   llvm::Value *llvm_eh_selector = PointerWidth == 32 ?
1308     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i32) :
1309     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
1310   llvm::Value *llvm_eh_typeid_for = PointerWidth == 32 ?
1311     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i32) :
1312     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
1313 
1314   // Exception object
1315   llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1316   llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1317 
1318   llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1319   llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1320 
1321   ESelArgs.push_back(Exc);
1322   ESelArgs.push_back(Personality);
1323 
1324   bool HasCatchAll = false;
1325   // Only @try blocks are allowed @catch blocks, but both can have @finally
1326   if (isTry) {
1327     if (const ObjCAtCatchStmt* CatchStmt =
1328       cast<ObjCAtTryStmt>(S).getCatchStmts())  {
1329       CGF.setInvokeDest(CatchInCatch);
1330 
1331       for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1332         const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
1333         Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
1334 
1335         // @catch() and @catch(id) both catch any ObjC exception
1336         if (!CatchDecl || CGF.getContext().isObjCIdType(CatchDecl->getType())
1337             || CatchDecl->getType()->isObjCQualifiedIdType()) {
1338           // Use i8* null here to signal this is a catch all, not a cleanup.
1339           ESelArgs.push_back(NULLPtr);
1340           HasCatchAll = true;
1341           // No further catches after this one will ever by reached
1342           break;
1343         }
1344 
1345         // All other types should be Objective-C interface pointer types.
1346         const PointerType *PT = CatchDecl->getType()->getAsPointerType();
1347         assert(PT && "Invalid @catch type.");
1348         const ObjCInterfaceType *IT =
1349           PT->getPointeeType()->getAsObjCInterfaceType();
1350         assert(IT && "Invalid @catch type.");
1351         llvm::Value *EHType =
1352           MakeConstantString(IT->getDecl()->getNameAsString());
1353         ESelArgs.push_back(EHType);
1354       }
1355     }
1356   }
1357 
1358   // We use a cleanup unless there was already a catch all.
1359   if (!HasCatchAll) {
1360     ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
1361     Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1362   }
1363 
1364   // Find which handler was matched.
1365   llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
1366       ESelArgs.begin(), ESelArgs.end(), "selector");
1367 
1368   for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1369     const ParmVarDecl *CatchParam = Handlers[i].first;
1370     const Stmt *CatchBody = Handlers[i].second;
1371 
1372     llvm::BasicBlock *Next = 0;
1373 
1374     // The last handler always matches.
1375     if (i + 1 != e) {
1376       assert(CatchParam && "Only last handler can be a catch all.");
1377 
1378       // Test whether this block matches the type for the selector and branch
1379       // to Match if it does, or to the next BB if it doesn't.
1380       llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1381       Next = CGF.createBasicBlock("catch.next");
1382       llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
1383           CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1384       CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1385           Next);
1386 
1387       CGF.EmitBlock(Match);
1388     }
1389 
1390     if (CatchBody) {
1391       llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1392           CGF.ConvertType(CatchParam->getType()));
1393 
1394       // Bind the catch parameter if it exists.
1395       if (CatchParam) {
1396         // CatchParam is a ParmVarDecl because of the grammar
1397         // construction used to handle this, but for codegen purposes
1398         // we treat this as a local decl.
1399         CGF.EmitLocalBlockVarDecl(*CatchParam);
1400         CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1401       }
1402 
1403       CGF.ObjCEHValueStack.push_back(ExcObject);
1404       CGF.EmitStmt(CatchBody);
1405       CGF.ObjCEHValueStack.pop_back();
1406 
1407       CGF.EmitBranchThroughCleanup(FinallyEnd);
1408 
1409       if (Next)
1410         CGF.EmitBlock(Next);
1411     } else {
1412       assert(!Next && "catchup should be last handler.");
1413 
1414       CGF.Builder.CreateStore(Exc, RethrowPtr);
1415       CGF.EmitBranchThroughCleanup(FinallyRethrow);
1416     }
1417   }
1418   // The @finally block is a secondary landing pad for any exceptions thrown in
1419   // @catch() blocks
1420   CGF.EmitBlock(CatchInCatch);
1421   Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1422   ESelArgs.clear();
1423   ESelArgs.push_back(Exc);
1424   ESelArgs.push_back(Personality);
1425   ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
1426   CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1427       "selector");
1428   CGF.Builder.CreateCall(llvm_eh_typeid_for,
1429       CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1430   CGF.Builder.CreateStore(Exc, RethrowPtr);
1431   CGF.EmitBranchThroughCleanup(FinallyRethrow);
1432 
1433   CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1434 
1435   CGF.setInvokeDest(PrevLandingPad);
1436 
1437   CGF.EmitBlock(FinallyBlock);
1438 
1439 
1440   if (isTry) {
1441     if (const ObjCAtFinallyStmt* FinallyStmt =
1442         cast<ObjCAtTryStmt>(S).getFinallyStmt())
1443       CGF.EmitStmt(FinallyStmt->getFinallyBody());
1444   } else {
1445     // Emit 'objc_sync_exit(expr)' as finally's sole statement for
1446     // @synchronized.
1447     std::vector<const llvm::Type*> Args(1, IdTy);
1448     llvm::FunctionType *FTy =
1449       llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
1450     llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
1451     llvm::Value *SyncArg =
1452       CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1453     SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1454     CGF.Builder.CreateCall(SyncExit, SyncArg);
1455   }
1456 
1457   if (Info.SwitchBlock)
1458     CGF.EmitBlock(Info.SwitchBlock);
1459   if (Info.EndBlock)
1460     CGF.EmitBlock(Info.EndBlock);
1461 
1462   // Branch around the rethrow code.
1463   CGF.EmitBranch(FinallyEnd);
1464 
1465   CGF.EmitBlock(FinallyRethrow);
1466   CGF.Builder.CreateCall(RethrowFn, CGF.Builder.CreateLoad(RethrowPtr));
1467   CGF.Builder.CreateUnreachable();
1468 
1469   CGF.EmitBlock(FinallyEnd);
1470 
1471 }
1472 
1473 void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1474                               const ObjCAtThrowStmt &S) {
1475   llvm::Value *ExceptionAsObject;
1476 
1477   std::vector<const llvm::Type*> Args(1, IdTy);
1478   llvm::FunctionType *FTy =
1479     llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
1480   llvm::Value *ThrowFn =
1481     CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
1482 
1483   if (const Expr *ThrowExpr = S.getThrowExpr()) {
1484     llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1485     ExceptionAsObject = Exception;
1486   } else {
1487     assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
1488            "Unexpected rethrow outside @catch block.");
1489     ExceptionAsObject = CGF.ObjCEHValueStack.back();
1490   }
1491   ExceptionAsObject =
1492       CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
1493 
1494   // Note: This may have to be an invoke, if we want to support constructs like:
1495   // @try {
1496   //  @throw(obj);
1497   // }
1498   // @catch(id) ...
1499   //
1500   // This is effectively turning @throw into an incredibly-expensive goto, but
1501   // it may happen as a result of inlining followed by missed optimizations, or
1502   // as a result of stupidity.
1503   llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1504   if (!UnwindBB) {
1505     CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1506     CGF.Builder.CreateUnreachable();
1507   } else {
1508     CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1509         &ExceptionAsObject+1);
1510   }
1511   // Clear the insertion point to indicate we are in unreachable code.
1512   CGF.Builder.ClearInsertionPoint();
1513 }
1514 
1515 llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
1516                                           llvm::Value *AddrWeakObj)
1517 {
1518   return 0;
1519 }
1520 
1521 void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1522                                    llvm::Value *src, llvm::Value *dst)
1523 {
1524   return;
1525 }
1526 
1527 void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1528                                      llvm::Value *src, llvm::Value *dst)
1529 {
1530   return;
1531 }
1532 
1533 void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1534                                    llvm::Value *src, llvm::Value *dst)
1535 {
1536   return;
1537 }
1538 
1539 void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1540                                          llvm::Value *src, llvm::Value *dst)
1541 {
1542   return;
1543 }
1544 
1545 llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1546                               const ObjCInterfaceDecl *ID,
1547                               const ObjCIvarDecl *Ivar) {
1548   const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1549     + '.' + Ivar->getNameAsString();
1550   // Emit the variable and initialize it with what we think the correct value
1551   // is.  This allows code compiled with non-fragile ivars to work correctly
1552   // when linked against code which isn't (most of the time).
1553   llvm::GlobalVariable *IvarOffsetGV = CGM.getModule().getGlobalVariable(Name);
1554   if (!IvarOffsetGV) {
1555     uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1556     llvm::ConstantInt *OffsetGuess =
1557       llvm::ConstantInt::get(LongTy, Offset, "ivar");
1558     IvarOffsetGV = new llvm::GlobalVariable(LongTy, false,
1559         llvm::GlobalValue::CommonLinkage, OffsetGuess, Name, &TheModule);
1560   }
1561   return IvarOffsetGV;
1562 }
1563 
1564 LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1565                                        QualType ObjectTy,
1566                                        llvm::Value *BaseValue,
1567                                        const ObjCIvarDecl *Ivar,
1568                                        unsigned CVRQualifiers) {
1569   const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
1570   return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1571                                   EmitIvarOffset(CGF, ID, Ivar));
1572 }
1573 static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1574                                                   const ObjCInterfaceDecl *OID,
1575                                                   const ObjCIvarDecl *OIVD) {
1576   llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
1577   Context.ShallowCollectObjCIvars(OID, Ivars);
1578   for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1579     if (OIVD == Ivars[k])
1580       return OID;
1581   }
1582 
1583   // Otherwise check in the super class.
1584   if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1585     return FindIvarInterface(Context, Super, OIVD);
1586 
1587   return 0;
1588 }
1589 
1590 llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1591                          const ObjCInterfaceDecl *Interface,
1592                          const ObjCIvarDecl *Ivar) {
1593   if (CGF.getContext().getLangOptions().ObjCNonFragileABI)
1594   {
1595     Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
1596     return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
1597         false, "ivar");
1598   }
1599   uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
1600   return llvm::ConstantInt::get(LongTy, Offset, "ivar");
1601 }
1602 
1603 CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
1604   return new CGObjCGNU(CGM);
1605 }
1606