blob: a8792bde955eca40a5b2c548b632551924cab8cf [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-03-01 08:50:34 +00002//
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//
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000010// 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.
Chris Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000020
Chris Lattnerdce14062008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000026
27#include "llvm/Intrinsics.h"
Chris Lattner0f984262008-03-01 08:50:34 +000028#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000029#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000030#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000031#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000032#include "llvm/Target/TargetData.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000033
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000035
36
Chris Lattnerdce14062008-06-26 04:19:03 +000037using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000038using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000039using llvm::dyn_cast;
40
41// The version of the runtime that this class targets. Must match the version
42// in the runtime.
43static const int RuntimeVersion = 8;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +000044static const int NonFragileRuntimeVersion = 9;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000045static const int ProtocolVersion = 2;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +000046static const int NonFragileProtocolVersion = 3;
Chris Lattner0f984262008-03-01 08:50:34 +000047
Chris Lattner0f984262008-03-01 08:50:34 +000048namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000049class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000050private:
Chris Lattnerdce14062008-06-26 04:19:03 +000051 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000052 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000053 const llvm::PointerType *SelectorTy;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +000054 const llvm::IntegerType *Int8Ty;
Chris Lattnere160c9b2009-01-27 05:06:01 +000055 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000056 const llvm::FunctionType *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000057 const llvm::PointerType *IdTy;
David Chisnall0f436562009-08-17 16:35:33 +000058 QualType ASTIdTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000059 const llvm::IntegerType *IntTy;
60 const llvm::PointerType *PtrTy;
61 const llvm::IntegerType *LongTy;
62 const llvm::PointerType *PtrToIntTy;
Daniel Dunbar5efccb12009-05-04 15:31:17 +000063 llvm::GlobalAlias *ClassPtrAlias;
64 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000065 std::vector<llvm::Constant*> Classes;
66 std::vector<llvm::Constant*> Categories;
67 std::vector<llvm::Constant*> ConstantStrings;
68 llvm::Function *LoadFunction;
69 llvm::StringMap<llvm::Constant*> ExistingProtocols;
70 typedef std::pair<std::string, std::string> TypedSelector;
71 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
72 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
73 // Some zeros used for GEPs in lots of places.
74 llvm::Constant *Zeros[2];
75 llvm::Constant *NULLPtr;
Owen Andersona1cf15f2009-07-14 23:10:40 +000076 llvm::LLVMContext &VMContext;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000077private:
78 llvm::Constant *GenerateIvarList(
79 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
80 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
81 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
82 llvm::Constant *GenerateMethodList(const std::string &ClassName,
83 const std::string &CategoryName,
Mike Stump1eb44332009-09-09 15:08:12 +000084 const llvm::SmallVectorImpl<Selector> &MethodSels,
85 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000086 bool isClassMethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +000087 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +000088 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
89 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
90 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000091 llvm::Constant *GenerateProtocolList(
92 const llvm::SmallVectorImpl<std::string> &Protocols);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +000093 // To ensure that all protocols are seen by the runtime, we add a category on
94 // a class defined in the runtime, declaring no methods, but adopting the
95 // protocols.
96 void GenerateProtocolHolderCategory(void);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000097 llvm::Constant *GenerateClassStructure(
98 llvm::Constant *MetaClass,
99 llvm::Constant *SuperClass,
100 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000101 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000102 llvm::Constant *Version,
103 llvm::Constant *InstanceSize,
104 llvm::Constant *IVars,
105 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000106 llvm::Constant *Protocols,
107 llvm::Constant *IvarOffsets,
108 llvm::Constant *Properties);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000109 llvm::Constant *GenerateProtocolMethodList(
110 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
111 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
112 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
113 &Name="");
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000114 llvm::Constant *ExportUniqueString(const std::string &Str, const std::string
115 prefix);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000116 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
David Chisnall41d63ed2010-01-08 00:14:31 +0000117 std::vector<llvm::Constant*> &V, const std::string &Name="",
118 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000119 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
David Chisnall41d63ed2010-01-08 00:14:31 +0000120 std::vector<llvm::Constant*> &V, const std::string &Name="",
121 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000122 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
123 const ObjCIvarDecl *Ivar);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000124 void EmitClassRef(const std::string &className);
Chris Lattner0f984262008-03-01 08:50:34 +0000125public:
Chris Lattnerdce14062008-06-26 04:19:03 +0000126 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff33fdb732009-03-31 16:53:37 +0000127 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Mike Stump1eb44332009-09-09 15:08:12 +0000128 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000129 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000130 QualType ResultType,
131 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000132 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000133 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000134 const CallArgList &CallArgs,
135 const ObjCMethodDecl *Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000136 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000137 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000138 QualType ResultType,
139 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000140 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000141 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000142 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000143 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000144 const CallArgList &CallArgs,
145 const ObjCMethodDecl *Method);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000146 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000147 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000148 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000149 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
150 *Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
152 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000153 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000154 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
155 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000156 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000157 const ObjCProtocolDecl *PD);
158 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000159 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000160 virtual llvm::Function *GetPropertyGetFunction();
161 virtual llvm::Function *GetPropertySetFunction();
Daniel Dunbar309a4362009-07-24 07:40:24 +0000162 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000164 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
165 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000166 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
167 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000168 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000169 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000170 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
171 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000172 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
173 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000174 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000175 llvm::Value *src, llvm::Value *dest,
176 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000177 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
178 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000179 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +0000180 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000181 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000182 QualType Ty);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000183 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
184 QualType ObjectTy,
185 llvm::Value *BaseValue,
186 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000187 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000188 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000189 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000190 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000191};
192} // end anonymous namespace
193
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000194
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000195/// Emits a reference to a dummy variable which is emitted with each class.
196/// This ensures that a linker error will be generated when trying to link
197/// together modules where a referenced class is not defined.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000198void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000199 std::string symbolRef = "__objc_class_ref_" + className;
200 // Don't emit two copies of the same symbol
Mike Stumpbb1c8602009-07-31 21:31:32 +0000201 if (TheModule.getGlobalVariable(symbolRef))
202 return;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000203 std::string symbolName = "__objc_class_name_" + className;
204 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
205 if (!ClassSymbol) {
Owen Anderson1c431b32009-07-08 19:05:04 +0000206 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
207 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000208 }
Owen Anderson1c431b32009-07-08 19:05:04 +0000209 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerf35271b2009-08-05 05:25:18 +0000210 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000211}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000212
213static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000214 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000215}
216
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000217static std::string SymbolNameForMethod(const std::string &ClassName, const
218 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
219{
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000220 return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000221 (isClassMethod ? "+" : "-") + MethodName;
222}
223
Chris Lattnerdce14062008-06-26 04:19:03 +0000224CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000225 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000226 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000227 IntTy = cast<llvm::IntegerType>(
228 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
229 LongTy = cast<llvm::IntegerType>(
230 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000232 Int8Ty = llvm::Type::getInt8Ty(VMContext);
233 // C string type. Used in lots of places.
234 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
235
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000236 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000237 Zeros[1] = Zeros[0];
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000238 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner391d77a2008-03-30 23:03:07 +0000239 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000240 SelectorTy = cast<llvm::PointerType>(
241 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
242
Owen Anderson96e0fc72009-07-29 22:16:19 +0000243 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000244 PtrTy = PtrToInt8Ty;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattner391d77a2008-03-30 23:03:07 +0000246 // Object type
David Chisnall0f436562009-08-17 16:35:33 +0000247 ASTIdTy = CGM.getContext().getObjCIdType();
248 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner391d77a2008-03-30 23:03:07 +0000250 // IMP type
251 std::vector<const llvm::Type*> IMPArgs;
252 IMPArgs.push_back(IdTy);
253 IMPArgs.push_back(SelectorTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000254 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000255}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000256
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000257// This has to perform the lookup every time, since posing and related
258// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000259llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000260 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000261 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
David Chisnall41d63ed2010-01-08 00:14:31 +0000262 // With the incompatible ABI, this will need to be replaced with a direct
263 // reference to the class symbol. For the compatible nonfragile ABI we are
264 // still performing this lookup at run time but emitting the symbol for the
265 // class externally so that we can make the switch later.
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000266 EmitClassRef(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000267 ClassName = Builder.CreateStructGEP(ClassName, 0);
268
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000269 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000270 llvm::Constant *ClassLookupFn =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000271 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000272 Params,
273 true),
274 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000275 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000276}
277
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000278llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000279 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000280 if (US == 0)
Owen Anderson96e0fc72009-07-29 22:16:19 +0000281 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
David Chisnall0f436562009-08-17 16:35:33 +0000282 llvm::GlobalValue::PrivateLinkage,
283 ".objc_untyped_selector_alias"+Sel.getAsString(),
Chris Lattner8e67b632008-06-26 04:37:12 +0000284 NULL, &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner8e67b632008-06-26 04:37:12 +0000286 return Builder.CreateLoad(US);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000287}
288
289llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
290 *Method) {
291
292 std::string SelName = Method->getSelector().getAsString();
293 std::string SelTypes;
294 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
295 // Typed selectors
296 TypedSelector Selector = TypedSelector(SelName,
297 SelTypes);
298
299 // If it's already cached, return it.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000300 if (TypedSelectors[Selector]) {
301 return Builder.CreateLoad(TypedSelectors[Selector]);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000302 }
303
304 // If it isn't, cache it.
305 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000306 llvm::PointerType::getUnqual(SelectorTy),
David Chisnall0f436562009-08-17 16:35:33 +0000307 llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000308 NULL, &TheModule);
309 TypedSelectors[Selector] = Sel;
310
311 return Builder.CreateLoad(Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000312}
313
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000314llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
315 const std::string &Name) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000316 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Owen Anderson3c4972d2009-07-29 18:54:39 +0000317 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000318}
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000319llvm::Constant *CGObjCGNU::ExportUniqueString(const std::string &Str,
320 const std::string prefix) {
321 std::string name = prefix + Str;
322 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
323 if (!ConstStr) {
324 llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
325 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
326 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
327 }
328 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
329}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000330
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000331llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
David Chisnall41d63ed2010-01-08 00:14:31 +0000332 std::vector<llvm::Constant*> &V, const std::string &Name,
333 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson08e25242009-07-27 22:29:56 +0000334 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000335 return new llvm::GlobalVariable(TheModule, Ty, false,
336 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000337}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000338
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000339llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
David Chisnall41d63ed2010-01-08 00:14:31 +0000340 std::vector<llvm::Constant*> &V, const std::string &Name,
341 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson7db6d832009-07-28 18:33:04 +0000342 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000343 return new llvm::GlobalVariable(TheModule, Ty, false,
Mike Stumpbb1c8602009-07-31 21:31:32 +0000344 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000345}
346
347/// Generate an NSConstantString object.
348//TODO: In case there are any crazy people still using the GNU runtime without
349//an OpenStep implementation, this should let them select their own class for
350//constant strings.
Steve Naroff33fdb732009-03-31 16:53:37 +0000351llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
Mike Stump1eb44332009-09-09 15:08:12 +0000352 std::string Str(SL->getString()->getStrData(),
Steve Naroff33fdb732009-03-31 16:53:37 +0000353 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000354 std::vector<llvm::Constant*> Ivars;
355 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000356 Ivars.push_back(MakeConstantString(Str));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000357 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000358 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson47a434f2009-08-05 23:18:46 +0000359 llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000360 Ivars, ".objc_str");
361 ConstantStrings.push_back(
Owen Anderson3c4972d2009-07-29 18:54:39 +0000362 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000363 return ObjCStr;
364}
365
366///Generates a message send where the super is the receiver. This is a message
367///send to self with special delivery semantics indicating which class's method
368///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000369CodeGen::RValue
370CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000371 QualType ResultType,
372 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000373 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000374 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000375 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000376 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000377 const CallArgList &CallArgs,
378 const ObjCMethodDecl *Method) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000379 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
380
381 CallArgList ActualArgs;
382
383 ActualArgs.push_back(
David Chisnall0f436562009-08-17 16:35:33 +0000384 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
385 ASTIdTy));
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000386 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
387 CGF.getContext().getObjCSelType()));
388 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
389
390 CodeGenTypes &Types = CGM.getTypes();
391 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbar67939662009-09-17 04:01:40 +0000392 const llvm::FunctionType *impType =
393 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000394
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000395 llvm::Value *ReceiverClass = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000396 if (isCategoryImpl) {
397 llvm::Constant *classLookupFunction = 0;
398 std::vector<const llvm::Type*> Params;
399 Params.push_back(PtrTy);
400 if (IsClassMessage) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000401 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000402 IdTy, Params, true), "objc_get_meta_class");
403 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000404 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000405 IdTy, Params, true), "objc_get_class");
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000406 }
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000407 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
408 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000409 } else {
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000410 // Set up global aliases for the metaclass or class pointer if they do not
411 // already exist. These will are forward-references which will be set to
Mike Stumpbb1c8602009-07-31 21:31:32 +0000412 // pointers to the class and metaclass structure created for the runtime
413 // load function. To send a message to super, we look up the value of the
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000414 // super_class pointer from either the class or metaclass structure.
415 if (IsClassMessage) {
416 if (!MetaClassPtrAlias) {
417 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
418 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
419 Class->getNameAsString(), NULL, &TheModule);
420 }
421 ReceiverClass = MetaClassPtrAlias;
422 } else {
423 if (!ClassPtrAlias) {
424 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
425 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
426 Class->getNameAsString(), NULL, &TheModule);
427 }
428 ReceiverClass = ClassPtrAlias;
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000429 }
Chris Lattner71238f62009-04-25 23:19:45 +0000430 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000431 // Cast the pointer to a simplified version of the class structure
Mike Stump1eb44332009-09-09 15:08:12 +0000432 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000433 llvm::PointerType::getUnqual(
Owen Anderson47a434f2009-08-05 23:18:46 +0000434 llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000435 // Get the superclass pointer
436 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
437 // Load the superclass pointer
438 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000439 // Construct the structure used to look up the IMP
Owen Anderson47a434f2009-08-05 23:18:46 +0000440 llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
441 Receiver->getType(), IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000442 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000443
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000444 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000445 CGF.Builder.CreateStore(ReceiverClass,
446 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000447
448 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000449 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000450 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000451 Params.push_back(SelectorTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000452 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000453 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
454 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000455 "objc_msg_lookup_super");
456
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000457 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000458 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000459 lookupArgs+2);
460
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000461 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000462}
463
Mike Stump1eb44332009-09-09 15:08:12 +0000464/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000465CodeGen::RValue
466CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000467 QualType ResultType,
468 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000469 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000470 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000471 const CallArgList &CallArgs,
472 const ObjCMethodDecl *Method) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000473 CGBuilderTy &Builder = CGF.Builder;
David Chisnall0f436562009-08-17 16:35:33 +0000474 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000475 llvm::Value *cmd;
476 if (Method)
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000477 cmd = GetSelector(Builder, Method);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000478 else
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000479 cmd = GetSelector(Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000480 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000481
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000482 Receiver = Builder.CreateBitCast(Receiver, IdTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000483 ActualArgs.push_back(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000484 std::make_pair(RValue::get(Receiver), ASTIdTy));
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000485 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
486 CGF.getContext().getObjCSelType()));
487 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
488
489 CodeGenTypes &Types = CGM.getTypes();
490 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbar67939662009-09-17 04:01:40 +0000491 const llvm::FunctionType *impType =
492 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000493
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000494 llvm::Value *imp;
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000495 // For sender-aware dispatch, we pass the sender as the third argument to a
496 // lookup function. When sending messages from C code, the sender is nil.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000497 // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
498 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
499
500 std::vector<const llvm::Type*> Params;
501 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
502 Builder.CreateStore(Receiver, ReceiverPtr);
503 Params.push_back(ReceiverPtr->getType());
504 Params.push_back(SelectorTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000505 llvm::Value *self;
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000506
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000507 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
508 self = CGF.LoadObjCSelf();
509 } else {
Owen Anderson03e20502009-07-30 23:11:26 +0000510 self = llvm::ConstantPointerNull::get(IdTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000511 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000512
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000513 Params.push_back(self->getType());
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000514
515 // The lookup function returns a slot, which can be safely cached.
516 llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy,
517 IntTy, llvm::PointerType::getUnqual(impType), NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000518 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000519 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000520 llvm::PointerType::getUnqual(SlotTy), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000521 "objc_msg_lookup_sender");
522
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000523 // The lookup function is guaranteed not to capture the receiver pointer.
524 if (llvm::Function *LookupFn = dyn_cast<llvm::Function>(lookupFunction)) {
525 LookupFn->setDoesNotCapture(1);
526 }
527
528 llvm::Value *slot =
529 Builder.CreateCall3(lookupFunction, ReceiverPtr, cmd, self);
530 imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
531 // The lookup function may have changed the receiver, so make sure we use
532 // the new one.
533 ActualArgs[0] =
534 std::make_pair(RValue::get(Builder.CreateLoad(ReceiverPtr)), ASTIdTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000535 } else {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000536 std::vector<const llvm::Type*> Params;
537 Params.push_back(Receiver->getType());
538 Params.push_back(SelectorTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000539 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000540 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
541 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000542 "objc_msg_lookup");
543
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000544 imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000545 }
Chris Lattner3eae03e2008-05-06 00:56:42 +0000546
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000547 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000548}
549
Mike Stump1eb44332009-09-09 15:08:12 +0000550/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000551/// objc_category structures.
552llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Mike Stump1eb44332009-09-09 15:08:12 +0000553 const std::string &CategoryName,
554 const llvm::SmallVectorImpl<Selector> &MethodSels,
555 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000556 bool isClassMethodList) {
David Chisnall0f436562009-08-17 16:35:33 +0000557 if (MethodSels.empty())
558 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000559 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000560 llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000561 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
562 PtrToInt8Ty, // Method types
Owen Anderson96e0fc72009-07-29 22:16:19 +0000563 llvm::PointerType::getUnqual(IMPTy), //Method pointer
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000564 NULL);
565 std::vector<llvm::Constant*> Methods;
566 std::vector<llvm::Constant*> Elements;
567 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
568 Elements.clear();
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000569 if (llvm::Constant *Method =
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000570 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000571 MethodSels[i].getAsString(),
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000572 isClassMethodList))) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000573 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
574 Elements.push_back(C);
575 Elements.push_back(MethodTypes[i]);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000576 Method = llvm::ConstantExpr::getBitCast(Method,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000577 llvm::PointerType::getUnqual(IMPTy));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000578 Elements.push_back(Method);
Owen Anderson08e25242009-07-27 22:29:56 +0000579 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000580 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000581 }
582
583 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +0000584 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000585 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +0000586 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000587 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000588
589 // Structure containing list pointer, array and array count
590 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000591 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000592 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Owen Anderson47a434f2009-08-05 23:18:46 +0000593 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
Mike Stump1eb44332009-09-09 15:08:12 +0000594 NextPtrTy,
595 IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000596 ObjCMethodArrayTy,
597 NULL);
598 // Refine next pointer type to concrete type
599 llvm::cast<llvm::OpaqueType>(
600 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
601 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
602
603 Methods.clear();
Owen Anderson03e20502009-07-30 23:11:26 +0000604 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000605 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson0032b272009-08-13 21:57:51 +0000606 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000607 MethodTypes.size()));
608 Methods.push_back(MethodArray);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000610 // Create an instance of the structure
611 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
612}
613
614/// Generates an IvarList. Used in construction of a objc_class.
615llvm::Constant *CGObjCGNU::GenerateIvarList(
616 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
617 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
618 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
David Chisnall18044632009-11-16 19:05:54 +0000619 if (IvarNames.size() == 0)
620 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000621 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000622 llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000623 PtrToInt8Ty,
624 PtrToInt8Ty,
625 IntTy,
626 NULL);
627 std::vector<llvm::Constant*> Ivars;
628 std::vector<llvm::Constant*> Elements;
629 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
630 Elements.clear();
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000631 Elements.push_back(IvarNames[i]);
632 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000633 Elements.push_back(IvarOffsets[i]);
Owen Anderson08e25242009-07-27 22:29:56 +0000634 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000635 }
636
637 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +0000638 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000639 IvarNames.size());
640
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000642 Elements.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000643 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson7db6d832009-07-28 18:33:04 +0000644 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000645 // Structure containing array and array count
Owen Anderson47a434f2009-08-05 23:18:46 +0000646 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000647 ObjCIvarArrayTy,
648 NULL);
649
650 // Create an instance of the structure
651 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
652}
653
654/// Generate a class structure
655llvm::Constant *CGObjCGNU::GenerateClassStructure(
656 llvm::Constant *MetaClass,
657 llvm::Constant *SuperClass,
658 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000659 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 llvm::Constant *Version,
661 llvm::Constant *InstanceSize,
662 llvm::Constant *IVars,
663 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000664 llvm::Constant *Protocols,
665 llvm::Constant *IvarOffsets,
666 llvm::Constant *Properties) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000667 // Set up the class structure
668 // Note: Several of these are char*s when they should be ids. This is
669 // because the runtime performs this translation on load.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000670 //
671 // Fields marked New ABI are part of the GNUstep runtime. We emit them
672 // anyway; the classes will still work with the GNU runtime, they will just
673 // be ignored.
Owen Anderson47a434f2009-08-05 23:18:46 +0000674 llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000675 PtrToInt8Ty, // class_pointer
676 PtrToInt8Ty, // super_class
677 PtrToInt8Ty, // name
678 LongTy, // version
679 LongTy, // info
680 LongTy, // instance_size
681 IVars->getType(), // ivars
682 Methods->getType(), // methods
Mike Stump1eb44332009-09-09 15:08:12 +0000683 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000684 PtrTy, // dtable
685 PtrTy, // subclass_list
686 PtrTy, // sibling_class
687 PtrTy, // protocols
688 PtrTy, // gc_object_type
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000689 // New ABI:
690 LongTy, // abi_version
691 IvarOffsets->getType(), // ivar_offsets
692 Properties->getType(), // properties
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000693 NULL);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000694 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000695 // Fill in the structure
696 std::vector<llvm::Constant*> Elements;
Owen Anderson3c4972d2009-07-29 18:54:39 +0000697 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000698 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000699 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000700 Elements.push_back(Zero);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000701 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000702 Elements.push_back(InstanceSize);
703 Elements.push_back(IVars);
704 Elements.push_back(Methods);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000705 Elements.push_back(NULLPtr);
706 Elements.push_back(NULLPtr);
707 Elements.push_back(NULLPtr);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000708 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000709 Elements.push_back(NULLPtr);
710 Elements.push_back(Zero);
711 Elements.push_back(IvarOffsets);
712 Elements.push_back(Properties);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000713 // Create an instance of the structure
David Chisnall41d63ed2010-01-08 00:14:31 +0000714 // This is now an externally visible symbol, so that we can speed up class
715 // messages in the next ABI.
716 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name),
717 llvm::GlobalValue::ExternalLinkage);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000718}
719
720llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
721 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
722 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
Mike Stump1eb44332009-09-09 15:08:12 +0000723 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000724 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000725 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
726 PtrToInt8Ty,
727 NULL);
728 std::vector<llvm::Constant*> Methods;
729 std::vector<llvm::Constant*> Elements;
730 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
731 Elements.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000732 Elements.push_back(MethodNames[i]);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000733 Elements.push_back(MethodTypes[i]);
Owen Anderson08e25242009-07-27 22:29:56 +0000734 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000735 }
Owen Anderson96e0fc72009-07-29 22:16:19 +0000736 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000737 MethodNames.size());
Owen Anderson7db6d832009-07-28 18:33:04 +0000738 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpbb1c8602009-07-31 21:31:32 +0000739 Methods);
Owen Anderson47a434f2009-08-05 23:18:46 +0000740 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000741 IntTy, ObjCMethodArrayTy, NULL);
742 Methods.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000743 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000744 Methods.push_back(Array);
745 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
746}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000747
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000748// Create the protocol list structure used in classes, categories and so on
749llvm::Constant *CGObjCGNU::GenerateProtocolList(
750 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000751 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000752 Protocols.size());
Owen Anderson47a434f2009-08-05 23:18:46 +0000753 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000754 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
755 LongTy,//FIXME: Should be size_t
756 ProtocolArrayTy,
757 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000758 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000759 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
760 iter != endIter ; iter++) {
David Chisnallff80fab2009-11-20 14:50:59 +0000761 llvm::Constant *protocol = 0;
762 llvm::StringMap<llvm::Constant*>::iterator value =
763 ExistingProtocols.find(*iter);
764 if (value == ExistingProtocols.end()) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000765 protocol = GenerateEmptyProtocol(*iter);
David Chisnallff80fab2009-11-20 14:50:59 +0000766 } else {
767 protocol = value->getValue();
768 }
Owen Anderson3c4972d2009-07-29 18:54:39 +0000769 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000770 PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000771 Elements.push_back(Ptr);
772 }
Owen Anderson7db6d832009-07-28 18:33:04 +0000773 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000774 Elements);
775 Elements.clear();
776 Elements.push_back(NULLPtr);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000777 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000778 Elements.push_back(ProtocolArray);
779 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
780}
781
Mike Stump1eb44332009-09-09 15:08:12 +0000782llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000783 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000784 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Mike Stump1eb44332009-09-09 15:08:12 +0000785 const llvm::Type *T =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000786 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000787 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000788}
789
790llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
791 const std::string &ProtocolName) {
792 llvm::SmallVector<std::string, 0> EmptyStringVector;
793 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
794
795 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000796 llvm::Constant *MethodList =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000797 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
798 // Protocols are objects containing lists of the methods implemented and
799 // protocols adopted.
Owen Anderson47a434f2009-08-05 23:18:46 +0000800 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000801 PtrToInt8Ty,
802 ProtocolList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000803 MethodList->getType(),
804 MethodList->getType(),
805 MethodList->getType(),
806 MethodList->getType(),
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000807 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000808 std::vector<llvm::Constant*> Elements;
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000809 // The isa pointer must be set to a magic number so the runtime knows it's
810 // the correct layout.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000811 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
812 NonFragileProtocolVersion : ProtocolVersion;
Owen Anderson3c4972d2009-07-29 18:54:39 +0000813 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000814 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000815 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
816 Elements.push_back(ProtocolList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000817 Elements.push_back(MethodList);
818 Elements.push_back(MethodList);
819 Elements.push_back(MethodList);
820 Elements.push_back(MethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000821 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000822}
823
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000824void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
825 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000826 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000827 llvm::SmallVector<std::string, 16> Protocols;
828 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
829 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000830 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000831 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
832 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000833 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
834 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
836 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000837 std::string TypeStr;
838 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000839 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
840 InstanceMethodNames.push_back(
841 MakeConstantString((*iter)->getSelector().getAsString()));
842 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
843 } else {
844 OptionalInstanceMethodNames.push_back(
845 MakeConstantString((*iter)->getSelector().getAsString()));
846 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
847 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000848 }
849 // Collect information about class methods:
850 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
851 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000852 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
853 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +0000854 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000855 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
856 iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000857 std::string TypeStr;
858 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000859 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
860 ClassMethodNames.push_back(
861 MakeConstantString((*iter)->getSelector().getAsString()));
862 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
863 } else {
864 OptionalClassMethodNames.push_back(
865 MakeConstantString((*iter)->getSelector().getAsString()));
866 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
867 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000868 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000869
870 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
871 llvm::Constant *InstanceMethodList =
872 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
873 llvm::Constant *ClassMethodList =
874 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000875 llvm::Constant *OptionalInstanceMethodList =
876 GenerateProtocolMethodList(OptionalInstanceMethodNames,
877 OptionalInstanceMethodTypes);
878 llvm::Constant *OptionalClassMethodList =
879 GenerateProtocolMethodList(OptionalClassMethodNames,
880 OptionalClassMethodTypes);
881
882 // Property metadata: name, attributes, isSynthesized, setter name, setter
883 // types, getter name, getter types.
884 // The isSynthesized value is always set to 0 in a protocol. It exists to
885 // simplify the runtime library by allowing it to use the same data
886 // structures for protocol metadata everywhere.
887 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
888 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
889 PtrToInt8Ty, NULL);
890 std::vector<llvm::Constant*> Properties;
891 std::vector<llvm::Constant*> OptionalProperties;
892
893 // Add all of the property methods need adding to the method list and to the
894 // property metadata list.
895 for (ObjCContainerDecl::prop_iterator
896 iter = PD->prop_begin(), endIter = PD->prop_end();
897 iter != endIter ; iter++) {
898 std::vector<llvm::Constant*> Fields;
899 ObjCPropertyDecl *property = (*iter);
900
901 Fields.push_back(MakeConstantString(property->getNameAsString()));
902 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
903 property->getPropertyAttributes()));
904 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
905 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
906 std::string TypeStr;
907 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
908 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
909 InstanceMethodTypes.push_back(TypeEncoding);
910 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
911 Fields.push_back(TypeEncoding);
912 } else {
913 Fields.push_back(NULLPtr);
914 Fields.push_back(NULLPtr);
915 }
916 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
917 std::string TypeStr;
918 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
919 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
920 InstanceMethodTypes.push_back(TypeEncoding);
921 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
922 Fields.push_back(TypeEncoding);
923 } else {
924 Fields.push_back(NULLPtr);
925 Fields.push_back(NULLPtr);
926 }
927 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
928 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
929 } else {
930 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
931 }
932 }
933 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
934 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
935 llvm::Constant* PropertyListInitFields[] =
936 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
937
938 llvm::Constant *PropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000939 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000940 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
941 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
942 PropertyListInit, ".objc_property_list");
943
944 llvm::Constant *OptionalPropertyArray =
945 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
946 OptionalProperties.size()) , OptionalProperties);
947 llvm::Constant* OptionalPropertyListInitFields[] = {
948 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
949 OptionalPropertyArray };
950
951 llvm::Constant *OptionalPropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000952 llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000953 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
954 OptionalPropertyListInit->getType(), false,
955 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
956 ".objc_property_list");
957
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000958 // Protocols are objects containing lists of the methods implemented and
959 // protocols adopted.
Owen Anderson47a434f2009-08-05 23:18:46 +0000960 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000961 PtrToInt8Ty,
962 ProtocolList->getType(),
963 InstanceMethodList->getType(),
964 ClassMethodList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000965 OptionalInstanceMethodList->getType(),
966 OptionalClassMethodList->getType(),
967 PropertyList->getType(),
968 OptionalPropertyList->getType(),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000969 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000970 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000971 // The isa pointer must be set to a magic number so the runtime knows it's
972 // the correct layout.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000973 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
974 NonFragileProtocolVersion : ProtocolVersion;
Owen Anderson3c4972d2009-07-29 18:54:39 +0000975 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000976 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000977 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
978 Elements.push_back(ProtocolList);
979 Elements.push_back(InstanceMethodList);
980 Elements.push_back(ClassMethodList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000981 Elements.push_back(OptionalInstanceMethodList);
982 Elements.push_back(OptionalClassMethodList);
983 Elements.push_back(PropertyList);
984 Elements.push_back(OptionalPropertyList);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 ExistingProtocols[ProtocolName] =
Owen Anderson3c4972d2009-07-29 18:54:39 +0000986 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000987 ".objc_protocol"), IdTy);
988}
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000989void CGObjCGNU::GenerateProtocolHolderCategory(void) {
990 // Collect information about instance methods
991 llvm::SmallVector<Selector, 1> MethodSels;
992 llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
993
994 std::vector<llvm::Constant*> Elements;
995 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
996 const std::string CategoryName = "AnotherHack";
997 Elements.push_back(MakeConstantString(CategoryName));
998 Elements.push_back(MakeConstantString(ClassName));
999 // Instance method list
1000 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1001 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1002 // Class method list
1003 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1004 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1005 // Protocol list
1006 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1007 ExistingProtocols.size());
1008 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
1009 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1010 LongTy,//FIXME: Should be size_t
1011 ProtocolArrayTy,
1012 NULL);
1013 std::vector<llvm::Constant*> ProtocolElements;
1014 for (llvm::StringMapIterator<llvm::Constant*> iter =
1015 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1016 iter != endIter ; iter++) {
1017 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1018 PtrTy);
1019 ProtocolElements.push_back(Ptr);
1020 }
1021 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1022 ProtocolElements);
1023 ProtocolElements.clear();
1024 ProtocolElements.push_back(NULLPtr);
1025 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1026 ExistingProtocols.size()));
1027 ProtocolElements.push_back(ProtocolArray);
1028 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1029 ProtocolElements, ".objc_protocol_list"), PtrTy));
1030 Categories.push_back(llvm::ConstantExpr::getBitCast(
1031 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
1032 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1033}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001034
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001035void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001036 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1037 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001038 // Collect information about instance methods
1039 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1040 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001041 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001042 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001043 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001044 InstanceMethodSels.push_back((*iter)->getSelector());
1045 std::string TypeStr;
1046 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001047 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001048 }
1049
1050 // Collect information about class methods
1051 llvm::SmallVector<Selector, 16> ClassMethodSels;
1052 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001053 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001054 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001055 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001056 ClassMethodSels.push_back((*iter)->getSelector());
1057 std::string TypeStr;
1058 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001059 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001060 }
1061
1062 // Collect the names of referenced protocols
1063 llvm::SmallVector<std::string, 16> Protocols;
1064 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
1065 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1066 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1067 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001068 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001069
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001070 std::vector<llvm::Constant*> Elements;
1071 Elements.push_back(MakeConstantString(CategoryName));
1072 Elements.push_back(MakeConstantString(ClassName));
Mike Stump1eb44332009-09-09 15:08:12 +00001073 // Instance method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001074 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001075 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001076 false), PtrTy));
1077 // Class method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001078 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001079 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001080 PtrTy));
1081 // Protocol list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001082 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001083 GenerateProtocolList(Protocols), PtrTy));
Owen Anderson3c4972d2009-07-29 18:54:39 +00001084 Categories.push_back(llvm::ConstantExpr::getBitCast(
Mike Stump1eb44332009-09-09 15:08:12 +00001085 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson47a434f2009-08-05 23:18:46 +00001086 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001087}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001088
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001089llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1090 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1091 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1092 ASTContext &Context = CGM.getContext();
1093 //
1094 // Property metadata: name, attributes, isSynthesized, setter name, setter
1095 // types, getter name, getter types.
1096 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1097 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1098 PtrToInt8Ty, NULL);
1099 std::vector<llvm::Constant*> Properties;
1100
1101
1102 // Add all of the property methods need adding to the method list and to the
1103 // property metadata list.
1104 for (ObjCImplDecl::propimpl_iterator
1105 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1106 iter != endIter ; iter++) {
1107 std::vector<llvm::Constant*> Fields;
1108 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
1109
1110 Fields.push_back(MakeConstantString(property->getNameAsString()));
1111 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1112 property->getPropertyAttributes()));
1113 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1114 (*iter)->getPropertyImplementation() ==
1115 ObjCPropertyImplDecl::Synthesize));
1116 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1117 InstanceMethodSels.push_back(getter->getSelector());
1118 std::string TypeStr;
1119 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1120 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1121 InstanceMethodTypes.push_back(TypeEncoding);
1122 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1123 Fields.push_back(TypeEncoding);
1124 } else {
1125 Fields.push_back(NULLPtr);
1126 Fields.push_back(NULLPtr);
1127 }
1128 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1129 InstanceMethodSels.push_back(setter->getSelector());
1130 std::string TypeStr;
1131 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1132 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1133 InstanceMethodTypes.push_back(TypeEncoding);
1134 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1135 Fields.push_back(TypeEncoding);
1136 } else {
1137 Fields.push_back(NULLPtr);
1138 Fields.push_back(NULLPtr);
1139 }
1140 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1141 }
1142 llvm::ArrayType *PropertyArrayTy =
1143 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1144 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1145 Properties);
1146 llvm::Constant* PropertyListInitFields[] =
1147 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1148
1149 llvm::Constant *PropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001150 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001151 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1152 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1153 ".objc_property_list");
1154}
1155
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001156void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1157 ASTContext &Context = CGM.getContext();
1158
1159 // Get the superclass name.
Mike Stump1eb44332009-09-09 15:08:12 +00001160 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001161 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001162 std::string SuperClassName;
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001163 if (SuperClassDecl) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001164 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001165 EmitClassRef(SuperClassName);
1166 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001167
1168 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +00001169 ObjCInterfaceDecl *ClassDecl =
1170 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001171 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001172 // Emit the symbol that is used to generate linker errors if this class is
1173 // referenced in other modules but not declared.
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001174 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump1eb44332009-09-09 15:08:12 +00001175 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001176 TheModule.getGlobalVariable(classSymbolName)) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001177 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001178 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00001179 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001180 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Anderson1c431b32009-07-08 19:05:04 +00001181 classSymbolName);
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001184 // Get the size of instances.
1185 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001186
1187 // Collect information about instance variables.
1188 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
1189 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
1190 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001192 std::vector<llvm::Constant*> IvarOffsetValues;
1193
Mike Stump1eb44332009-09-09 15:08:12 +00001194 int superInstanceSize = !SuperClassDecl ? 0 :
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001195 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
1196 // For non-fragile ivars, set the instance size to 0 - {the size of just this
1197 // class}. The runtime will then set this to the correct value on load.
1198 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1199 instanceSize = 0 - (instanceSize - superInstanceSize);
1200 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001201 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1202 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1203 // Store the name
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001204 IvarNames.push_back(MakeConstantString((*iter)->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001205 // Get the type encoding for this ivar
1206 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001207 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001208 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001209 // Get the offset
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001210 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
David Chisnallaecbf242009-11-17 19:32:15 +00001211 uint64_t Offset = BaseOffset;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001212 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001213 Offset = BaseOffset - superInstanceSize;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001214 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001215 IvarOffsets.push_back(
Owen Anderson0032b272009-08-13 21:57:51 +00001216 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001217 IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1218 false, llvm::GlobalValue::ExternalLinkage,
1219 llvm::ConstantInt::get(IntTy, BaseOffset),
1220 "__objc_ivar_offset_value_" + ClassName +"." +
1221 (*iter)->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001222 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001223 llvm::Constant *IvarOffsetArrayInit =
1224 llvm::ConstantArray::get(llvm::ArrayType::get(PtrToIntTy,
1225 IvarOffsetValues.size()), IvarOffsetValues);
1226 llvm::GlobalVariable *IvarOffsetArray = new llvm::GlobalVariable(TheModule,
1227 IvarOffsetArrayInit->getType(), false,
1228 llvm::GlobalValue::InternalLinkage, IvarOffsetArrayInit,
1229 ".ivar.offsets");
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001230
1231 // Collect information about instance methods
1232 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1233 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001234 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001235 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001236 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001237 InstanceMethodSels.push_back((*iter)->getSelector());
1238 std::string TypeStr;
1239 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001240 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001241 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001242
1243 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1244 InstanceMethodTypes);
1245
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001246
1247 // Collect information about class methods
1248 llvm::SmallVector<Selector, 16> ClassMethodSels;
1249 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001250 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001251 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001252 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001253 ClassMethodSels.push_back((*iter)->getSelector());
1254 std::string TypeStr;
1255 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001256 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001257 }
1258 // Collect the names of referenced protocols
1259 llvm::SmallVector<std::string, 16> Protocols;
1260 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1261 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1262 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001263 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001264
1265
1266
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001267 // Get the superclass pointer.
1268 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +00001269 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001270 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
1271 } else {
Owen Anderson03e20502009-07-30 23:11:26 +00001272 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001273 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001274 // Empty vector used to construct empty method lists
1275 llvm::SmallVector<llvm::Constant*, 1> empty;
1276 // Generate the method and instance variable lists
1277 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00001278 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001279 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00001280 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001281 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
1282 IvarOffsets);
Mike Stump1eb44332009-09-09 15:08:12 +00001283 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001284 // we emit a symbol containing the offset for each ivar in the class. This
1285 // allows code compiled for the non-Fragile ABI to inherit from code compiled
1286 // for the legacy ABI, without causing problems. The converse is also
1287 // possible, but causes all ivar accesses to be fragile.
1288 int i = 0;
1289 // Offset pointer for getting at the correct field in the ivar list when
1290 // setting up the alias. These are: The base address for the global, the
1291 // ivar array (second field), the ivar in this list (set for each ivar), and
1292 // the offset (third field in ivar structure)
1293 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
1294 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001295 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001296 llvm::ConstantInt::get(IndexTy, 2) };
1297
1298 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1299 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1300 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1301 +(*iter)->getNameAsString();
1302 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++);
1303 // Get the correct ivar field
1304 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1305 IvarList, offsetPointerIndexes, 4);
1306 // Get the existing alias, if one exists.
1307 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1308 if (offset) {
1309 offset->setInitializer(offsetValue);
1310 // If this is the real definition, change its linkage type so that
1311 // different modules will use this one, rather than their private
1312 // copy.
1313 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1314 } else {
1315 // Add a new alias if there isn't one already.
1316 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1317 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1318 }
1319 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001320 //Generate metaclass for class methods
1321 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnall18044632009-11-16 19:05:54 +00001322 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001323 empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001324
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001325 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +00001326 llvm::Constant *ClassStruct =
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001327 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner8ec03f52008-11-24 03:54:41 +00001328 ClassName.c_str(), 0,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001329 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001330 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1331 Properties);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001332
1333 // Resolve the class aliases, if they exist.
1334 if (ClassPtrAlias) {
1335 ClassPtrAlias->setAliasee(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001336 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001337 ClassPtrAlias = 0;
1338 }
1339 if (MetaClassPtrAlias) {
1340 MetaClassPtrAlias->setAliasee(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001341 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001342 MetaClassPtrAlias = 0;
1343 }
1344
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001345 // Add class structure to list to be added to the symtab later
Owen Anderson3c4972d2009-07-29 18:54:39 +00001346 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001347 Classes.push_back(ClassStruct);
1348}
1349
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00001350
Mike Stump1eb44332009-09-09 15:08:12 +00001351llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001352 // Only emit an ObjC load function if no Objective-C stuff has been called
1353 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1354 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +00001355 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001356 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +00001357
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001358 // Add all referenced protocols to a category.
1359 GenerateProtocolHolderCategory();
1360
Chris Lattnere160c9b2009-01-27 05:06:01 +00001361 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1362 SelectorTy->getElementType());
1363 const llvm::Type *SelStructPtrTy = SelectorTy;
1364 bool isSelOpaque = false;
1365 if (SelStructTy == 0) {
Owen Anderson47a434f2009-08-05 23:18:46 +00001366 SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
1367 PtrToInt8Ty, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001368 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +00001369 isSelOpaque = true;
1370 }
1371
Eli Friedman1b8956e2008-06-01 16:00:02 +00001372 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +00001373 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +00001374 TheModule.addTypeName(".objc_id", IdTy);
1375 TheModule.addTypeName(".objc_imp", IMPTy);
1376
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001377 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +00001378 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001379 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +00001380 if (ConstantStrings.size()) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00001381 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattner71238f62009-04-25 23:19:45 +00001382 ConstantStrings.size() + 1);
1383 ConstantStrings.push_back(NULLPtr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001384
Daniel Dunbar1b096952009-11-29 02:38:47 +00001385 llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
1386 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001387 Elements.push_back(MakeConstantString(StringClass,
1388 ".objc_static_class_name"));
Owen Anderson7db6d832009-07-28 18:33:04 +00001389 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattner71238f62009-04-25 23:19:45 +00001390 ConstantStrings));
Mike Stump1eb44332009-09-09 15:08:12 +00001391 llvm::StructType *StaticsListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00001392 llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001393 llvm::Type *StaticsListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001394 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001395 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump1eb44332009-09-09 15:08:12 +00001396 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001397 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner71238f62009-04-25 23:19:45 +00001398 Elements.clear();
1399 Elements.push_back(Statics);
Owen Andersonc9c88b42009-07-31 20:28:54 +00001400 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner71238f62009-04-25 23:19:45 +00001401 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001402 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001403 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001404 // Array of classes, categories, and constant objects
Owen Anderson96e0fc72009-07-29 22:16:19 +00001405 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001406 Classes.size() + Categories.size() + 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001407 llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00001408 LongTy, SelStructPtrTy,
Owen Anderson0032b272009-08-13 21:57:51 +00001409 llvm::Type::getInt16Ty(VMContext),
1410 llvm::Type::getInt16Ty(VMContext),
Chris Lattner630404b2008-06-26 04:10:42 +00001411 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001412
1413 Elements.clear();
1414 // Pointer to an array of selectors used in this module.
1415 std::vector<llvm::Constant*> Selectors;
1416 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1417 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1418 iter != iterEnd ; ++iter) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001419 Elements.push_back(ExportUniqueString(iter->first.first, ".objc_sel_name"));
David Chisnalla7c5b082009-09-14 19:04:10 +00001420 Elements.push_back(MakeConstantString(iter->first.second,
Chris Lattner630404b2008-06-26 04:10:42 +00001421 ".objc_sel_types"));
Owen Anderson08e25242009-07-27 22:29:56 +00001422 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001423 Elements.clear();
1424 }
1425 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1426 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +00001427 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001428 Elements.push_back(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001429 ExportUniqueString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001430 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00001431 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001432 Elements.clear();
1433 }
1434 Elements.push_back(NULLPtr);
1435 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00001436 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001437 Elements.clear();
1438 // Number of static selectors
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001439 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001440 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson96e0fc72009-07-29 22:16:19 +00001441 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001442 ".objc_selector_list");
Mike Stump1eb44332009-09-09 15:08:12 +00001443 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattnere160c9b2009-01-27 05:06:01 +00001444 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001445
1446 // Now that all of the static selectors exist, create pointers to them.
1447 int index = 0;
1448 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1449 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1450 iter != iterEnd; ++iter) {
1451 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson0032b272009-08-13 21:57:51 +00001452 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Owen Anderson1c431b32009-07-08 19:05:04 +00001453 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1454 true, llvm::GlobalValue::InternalLinkage,
Owen Anderson3c4972d2009-07-29 18:54:39 +00001455 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson1c431b32009-07-08 19:05:04 +00001456 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001457 // If selectors are defined as an opaque type, cast the pointer to this
1458 // type.
1459 if (isSelOpaque) {
Owen Anderson3c4972d2009-07-29 18:54:39 +00001460 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001461 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001462 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001463 (*iter).second->setAliasee(SelPtr);
1464 }
1465 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1466 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1467 iter != iterEnd; iter++) {
1468 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson0032b272009-08-13 21:57:51 +00001469 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Mike Stumpbb1c8602009-07-31 21:31:32 +00001470 llvm::Constant *SelPtr = new llvm::GlobalVariable
Mike Stump1eb44332009-09-09 15:08:12 +00001471 (TheModule, SelStructPtrTy,
Mike Stumpbb1c8602009-07-31 21:31:32 +00001472 true, llvm::GlobalValue::InternalLinkage,
1473 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1474 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001475 // If selectors are defined as an opaque type, cast the pointer to this
1476 // type.
1477 if (isSelOpaque) {
Owen Anderson3c4972d2009-07-29 18:54:39 +00001478 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001479 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001480 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001481 (*iter).second->setAliasee(SelPtr);
1482 }
1483 // Number of classes defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001484 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001485 Classes.size()));
1486 // Number of categories defined
Mike Stump1eb44332009-09-09 15:08:12 +00001487 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001488 Categories.size()));
1489 // Create an array of classes, then categories, then static object instances
1490 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1491 // NULL-terminated list of static object instances (mainly constant strings)
1492 Classes.push_back(Statics);
1493 Classes.push_back(NULLPtr);
Owen Anderson7db6d832009-07-28 18:33:04 +00001494 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001495 Elements.push_back(ClassList);
Mike Stump1eb44332009-09-09 15:08:12 +00001496 // Construct the symbol table
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001497 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1498
1499 // The symbol table is contained in a module which has some version-checking
1500 // constants
Owen Anderson47a434f2009-08-05 23:18:46 +00001501 llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001502 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001503 Elements.clear();
1504 // Runtime version used for compatibility checking.
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001505 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Mike Stump1eb44332009-09-09 15:08:12 +00001506 Elements.push_back(llvm::ConstantInt::get(LongTy,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001507 NonFragileRuntimeVersion));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001508 } else {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001509 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001510 }
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00001511 // sizeof(ModuleTy)
1512 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001513 Elements.push_back(llvm::ConstantInt::get(LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001514 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001515 //FIXME: Should be the path to the file where this module was declared
1516 Elements.push_back(NULLPtr);
1517 Elements.push_back(SymTab);
1518 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1519
1520 // Create the load function calling the runtime entry point with the module
1521 // structure
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001522 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson0032b272009-08-13 21:57:51 +00001523 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001524 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1525 &TheModule);
Owen Anderson0032b272009-08-13 21:57:51 +00001526 llvm::BasicBlock *EntryBB =
1527 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001528 CGBuilderTy Builder(VMContext);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001529 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001530
1531 std::vector<const llvm::Type*> Params(1,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001532 llvm::PointerType::getUnqual(ModuleTy));
1533 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson0032b272009-08-13 21:57:51 +00001534 llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001535 Builder.CreateCall(Register, Module);
1536 Builder.CreateRetVoid();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001537
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001538 return LoadFunction;
1539}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001540
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001541llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump1eb44332009-09-09 15:08:12 +00001542 const ObjCContainerDecl *CD) {
1543 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001544 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001545 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1546 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1547 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001548 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001549
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001550 CodeGenTypes &Types = CGM.getTypes();
Mike Stump1eb44332009-09-09 15:08:12 +00001551 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001552 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001553 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1554 MethodName, isClassMethod);
1555
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001556 llvm::Function *Method
Mike Stump1eb44332009-09-09 15:08:12 +00001557 = llvm::Function::Create(MethodTy,
1558 llvm::GlobalValue::InternalLinkage,
1559 FunctionName,
1560 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001561 return Method;
1562}
1563
Daniel Dunbar49f66022008-09-24 03:38:44 +00001564llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Mike Stump1eb44332009-09-09 15:08:12 +00001565 std::vector<const llvm::Type*> Params;
1566 const llvm::Type *BoolTy =
1567 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1568 Params.push_back(IdTy);
1569 Params.push_back(SelectorTy);
1570 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1571 Params.push_back(LongTy);
1572 Params.push_back(BoolTy);
1573 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1574 const llvm::FunctionType *FTy =
1575 llvm::FunctionType::get(IdTy, Params, false);
1576 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1577 "objc_getProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001578}
1579
1580llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Mike Stump1eb44332009-09-09 15:08:12 +00001581 std::vector<const llvm::Type*> Params;
1582 const llvm::Type *BoolTy =
1583 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1584 Params.push_back(IdTy);
1585 Params.push_back(SelectorTy);
1586 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1587 Params.push_back(LongTy);
1588 Params.push_back(IdTy);
1589 Params.push_back(BoolTy);
1590 Params.push_back(BoolTy);
1591 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1592 const llvm::FunctionType *FTy =
1593 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
1594 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1595 "objc_setProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001596}
1597
Daniel Dunbar309a4362009-07-24 07:40:24 +00001598llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
1599 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1600 ASTContext &Ctx = CGM.getContext();
1601 // void objc_enumerationMutation (id)
1602 llvm::SmallVector<QualType,16> Params;
David Chisnall0f436562009-08-17 16:35:33 +00001603 Params.push_back(ASTIdTy);
Daniel Dunbar309a4362009-07-24 07:40:24 +00001604 const llvm::FunctionType *FTy =
1605 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
1606 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001607}
1608
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001609void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1610 const Stmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001611 // Pointer to the personality function
1612 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +00001613 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +00001614 true),
Chris Lattner5dc08672009-05-08 00:11:50 +00001615 "__gnu_objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001616 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
Chris Lattner5dc08672009-05-08 00:11:50 +00001617 std::vector<const llvm::Type*> Params;
1618 Params.push_back(PtrTy);
1619 llvm::Value *RethrowFn =
Owen Anderson0032b272009-08-13 21:57:51 +00001620 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
David Chisnall90665bd2010-01-06 18:02:59 +00001621 Params, false), "_Unwind_Resume");
Chris Lattner5dc08672009-05-08 00:11:50 +00001622
1623 bool isTry = isa<ObjCAtTryStmt>(S);
1624 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1625 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1626 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattnerd6a99072009-05-11 18:16:28 +00001627 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattner5dc08672009-05-08 00:11:50 +00001628 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1629 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1630 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1631
David Chisnall0faa5162009-12-24 02:26:34 +00001632 // @synchronized()
Chris Lattner5dc08672009-05-08 00:11:50 +00001633 if (!isTry) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001634 std::vector<const llvm::Type*> Args(1, IdTy);
1635 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001636 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001637 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
Mike Stump1eb44332009-09-09 15:08:12 +00001638 llvm::Value *SyncArg =
Chris Lattnerd6a99072009-05-11 18:16:28 +00001639 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1640 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1641 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001642 }
1643
Chris Lattnerd6a99072009-05-11 18:16:28 +00001644
Chris Lattner5dc08672009-05-08 00:11:50 +00001645 // Push an EH context entry, used for handling rethrows and jumps
1646 // through finally.
1647 CGF.PushCleanupBlock(FinallyBlock);
1648
1649 // Emit the statements in the @try {} block
1650 CGF.setInvokeDest(TryHandler);
1651
1652 CGF.EmitBlock(TryBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001653 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
Chris Lattner5dc08672009-05-08 00:11:50 +00001654 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1655
1656 // Jump to @finally if there is no exception
1657 CGF.EmitBranchThroughCleanup(FinallyEnd);
1658
1659 // Emit the handlers
1660 CGF.EmitBlock(TryHandler);
1661
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001662 // Get the correct versions of the exception handling intrinsics
1663 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +00001664 llvm::Value *llvm_eh_exception =
Chris Lattner5dc08672009-05-08 00:11:50 +00001665 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Duncan Sands790b20e2009-10-14 16:13:30 +00001666 llvm::Value *llvm_eh_selector =
1667 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
1668 llvm::Value *llvm_eh_typeid_for =
1669 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Chris Lattner5dc08672009-05-08 00:11:50 +00001670
1671 // Exception object
1672 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1673 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1674
1675 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1676 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1677
1678 ESelArgs.push_back(Exc);
1679 ESelArgs.push_back(Personality);
1680
1681 bool HasCatchAll = false;
1682 // Only @try blocks are allowed @catch blocks, but both can have @finally
1683 if (isTry) {
1684 if (const ObjCAtCatchStmt* CatchStmt =
1685 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001686 CGF.setInvokeDest(CatchInCatch);
Chris Lattner5dc08672009-05-08 00:11:50 +00001687
1688 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1689 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Mike Stumpbb1c8602009-07-31 21:31:32 +00001690 Handlers.push_back(std::make_pair(CatchDecl,
1691 CatchStmt->getCatchBody()));
Chris Lattner5dc08672009-05-08 00:11:50 +00001692
1693 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff14108da2009-07-10 23:34:53 +00001694 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattner5dc08672009-05-08 00:11:50 +00001695 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1696 // Use i8* null here to signal this is a catch all, not a cleanup.
1697 ESelArgs.push_back(NULLPtr);
1698 HasCatchAll = true;
1699 // No further catches after this one will ever by reached
1700 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001701 }
Chris Lattner5dc08672009-05-08 00:11:50 +00001702
1703 // All other types should be Objective-C interface pointer types.
Mike Stump1eb44332009-09-09 15:08:12 +00001704 const ObjCObjectPointerType *OPT =
John McCall183700f2009-09-21 23:43:11 +00001705 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +00001706 assert(OPT && "Invalid @catch type.");
Mike Stump1eb44332009-09-09 15:08:12 +00001707 const ObjCInterfaceType *IT =
John McCall183700f2009-09-21 23:43:11 +00001708 OPT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner5dc08672009-05-08 00:11:50 +00001709 assert(IT && "Invalid @catch type.");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001710 llvm::Value *EHType =
1711 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattner5dc08672009-05-08 00:11:50 +00001712 ESelArgs.push_back(EHType);
1713 }
1714 }
1715 }
1716
1717 // We use a cleanup unless there was already a catch all.
1718 if (!HasCatchAll) {
Owen Anderson0032b272009-08-13 21:57:51 +00001719 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattner5dc08672009-05-08 00:11:50 +00001720 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1721 }
1722
1723 // Find which handler was matched.
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001724 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattner5dc08672009-05-08 00:11:50 +00001725 ESelArgs.begin(), ESelArgs.end(), "selector");
1726
1727 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1728 const ParmVarDecl *CatchParam = Handlers[i].first;
1729 const Stmt *CatchBody = Handlers[i].second;
1730
1731 llvm::BasicBlock *Next = 0;
1732
1733 // The last handler always matches.
1734 if (i + 1 != e) {
1735 assert(CatchParam && "Only last handler can be a catch all.");
1736
1737 // Test whether this block matches the type for the selector and branch
1738 // to Match if it does, or to the next BB if it doesn't.
1739 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1740 Next = CGF.createBasicBlock("catch.next");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001741 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattner5dc08672009-05-08 00:11:50 +00001742 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1743 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1744 Next);
1745
1746 CGF.EmitBlock(Match);
1747 }
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Chris Lattner5dc08672009-05-08 00:11:50 +00001749 if (CatchBody) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001750 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1751 CGF.ConvertType(CatchParam->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Chris Lattner5dc08672009-05-08 00:11:50 +00001753 // Bind the catch parameter if it exists.
1754 if (CatchParam) {
1755 // CatchParam is a ParmVarDecl because of the grammar
1756 // construction used to handle this, but for codegen purposes
1757 // we treat this as a local decl.
1758 CGF.EmitLocalBlockVarDecl(*CatchParam);
1759 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1760 }
1761
1762 CGF.ObjCEHValueStack.push_back(ExcObject);
1763 CGF.EmitStmt(CatchBody);
1764 CGF.ObjCEHValueStack.pop_back();
1765
1766 CGF.EmitBranchThroughCleanup(FinallyEnd);
1767
1768 if (Next)
1769 CGF.EmitBlock(Next);
1770 } else {
1771 assert(!Next && "catchup should be last handler.");
1772
1773 CGF.Builder.CreateStore(Exc, RethrowPtr);
1774 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1775 }
1776 }
Chris Lattnerd6a99072009-05-11 18:16:28 +00001777 // The @finally block is a secondary landing pad for any exceptions thrown in
1778 // @catch() blocks
1779 CGF.EmitBlock(CatchInCatch);
1780 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1781 ESelArgs.clear();
1782 ESelArgs.push_back(Exc);
1783 ESelArgs.push_back(Personality);
David Chisnall0faa5162009-12-24 02:26:34 +00001784 // If there is a @catch or @finally clause in outside of this one then we
1785 // need to make sure that we catch and rethrow it.
1786 if (PrevLandingPad) {
1787 ESelArgs.push_back(NULLPtr);
1788 } else {
1789 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
1790 }
Chris Lattnerd6a99072009-05-11 18:16:28 +00001791 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1792 "selector");
1793 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1794 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1795 CGF.Builder.CreateStore(Exc, RethrowPtr);
1796 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1797
Chris Lattner5dc08672009-05-08 00:11:50 +00001798 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1799
1800 CGF.setInvokeDest(PrevLandingPad);
1801
1802 CGF.EmitBlock(FinallyBlock);
1803
Chris Lattnerd6a99072009-05-11 18:16:28 +00001804
Chris Lattner5dc08672009-05-08 00:11:50 +00001805 if (isTry) {
Mike Stump1eb44332009-09-09 15:08:12 +00001806 if (const ObjCAtFinallyStmt* FinallyStmt =
Chris Lattner5dc08672009-05-08 00:11:50 +00001807 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1808 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1809 } else {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001810 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattner5dc08672009-05-08 00:11:50 +00001811 // @synchronized.
Chris Lattnerd6a99072009-05-11 18:16:28 +00001812 std::vector<const llvm::Type*> Args(1, IdTy);
1813 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001814 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001815 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Mike Stump1eb44332009-09-09 15:08:12 +00001816 llvm::Value *SyncArg =
Chris Lattnerd6a99072009-05-11 18:16:28 +00001817 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1818 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1819 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001820 }
1821
1822 if (Info.SwitchBlock)
1823 CGF.EmitBlock(Info.SwitchBlock);
1824 if (Info.EndBlock)
1825 CGF.EmitBlock(Info.EndBlock);
1826
1827 // Branch around the rethrow code.
1828 CGF.EmitBranch(FinallyEnd);
1829
1830 CGF.EmitBlock(FinallyRethrow);
David Chisnall0faa5162009-12-24 02:26:34 +00001831
1832 llvm::Value *ExceptionObject = CGF.Builder.CreateLoad(RethrowPtr);
1833 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1834 if (!UnwindBB) {
1835 CGF.Builder.CreateCall(RethrowFn, ExceptionObject);
1836 // Exception always thrown, next instruction is never reached.
1837 CGF.Builder.CreateUnreachable();
1838 } else {
1839 // If there is a @catch block outside this scope, we invoke instead of
1840 // calling because we may return to this function. This is very slow, but
1841 // some people still do it. It would be nice to add an optimised path for
1842 // this.
1843 CGF.Builder.CreateInvoke(RethrowFn, UnwindBB, UnwindBB, &ExceptionObject,
1844 &ExceptionObject+1);
1845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Chris Lattner5dc08672009-05-08 00:11:50 +00001847 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001848}
1849
1850void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001851 const ObjCAtThrowStmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001852 llvm::Value *ExceptionAsObject;
1853
1854 std::vector<const llvm::Type*> Args(1, IdTy);
1855 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001856 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001857 llvm::Value *ThrowFn =
Chris Lattner5dc08672009-05-08 00:11:50 +00001858 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Chris Lattner5dc08672009-05-08 00:11:50 +00001860 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1861 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001862 ExceptionAsObject = Exception;
Chris Lattner5dc08672009-05-08 00:11:50 +00001863 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001864 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattner5dc08672009-05-08 00:11:50 +00001865 "Unexpected rethrow outside @catch block.");
1866 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1867 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001868 ExceptionAsObject =
1869 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Chris Lattner5dc08672009-05-08 00:11:50 +00001871 // Note: This may have to be an invoke, if we want to support constructs like:
1872 // @try {
1873 // @throw(obj);
1874 // }
1875 // @catch(id) ...
1876 //
1877 // This is effectively turning @throw into an incredibly-expensive goto, but
1878 // it may happen as a result of inlining followed by missed optimizations, or
1879 // as a result of stupidity.
1880 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1881 if (!UnwindBB) {
1882 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1883 CGF.Builder.CreateUnreachable();
1884 } else {
1885 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1886 &ExceptionAsObject+1);
1887 }
1888 // Clear the insertion point to indicate we are in unreachable code.
1889 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001890}
1891
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001892llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00001893 llvm::Value *AddrWeakObj) {
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001894 return 0;
1895}
1896
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001897void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00001898 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001899 return;
1900}
1901
Fariborz Jahanian58626502008-11-19 00:59:10 +00001902void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00001903 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian58626502008-11-19 00:59:10 +00001904 return;
1905}
1906
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001907void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001908 llvm::Value *src, llvm::Value *dst,
1909 llvm::Value *ivarOffset) {
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001910 return;
1911}
1912
Fariborz Jahanian58626502008-11-19 00:59:10 +00001913void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00001914 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian58626502008-11-19 00:59:10 +00001915 return;
1916}
1917
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001918void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00001919 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001920 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00001921 QualType Ty) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001922 return;
1923}
1924
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001925llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1926 const ObjCInterfaceDecl *ID,
1927 const ObjCIvarDecl *Ivar) {
1928 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1929 + '.' + Ivar->getNameAsString();
1930 // Emit the variable and initialize it with what we think the correct value
1931 // is. This allows code compiled with non-fragile ivars to work correctly
1932 // when linked against code which isn't (most of the time).
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001933 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1934 if (!IvarOffsetPointer) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001935 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1936 llvm::ConstantInt *OffsetGuess =
David Chisnallf9508372010-01-11 19:02:35 +00001937 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001938 // Don't emit the guess in non-PIC code because the linker will not be able
1939 // to replace it with the real version for a library. In non-PIC code you
1940 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump1eb44332009-09-09 15:08:12 +00001941 // GCC-compiled class.
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001942 if (CGM.getLangOptions().PICLevel) {
1943 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
1944 llvm::Type::getInt32Ty(VMContext), false,
1945 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
1946 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
1947 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
1948 IvarOffsetGV, Name);
1949 } else {
1950 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001951 llvm::Type::getInt32PtrTy(VMContext), false,
1952 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001953 }
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001954 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001955 return IvarOffsetPointer;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001956}
1957
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001958LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1959 QualType ObjectTy,
1960 llvm::Value *BaseValue,
1961 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001962 unsigned CVRQualifiers) {
John McCall183700f2009-09-21 23:43:11 +00001963 const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00001964 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1965 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001966}
Mike Stumpbb1c8602009-07-31 21:31:32 +00001967
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001968static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1969 const ObjCInterfaceDecl *OID,
1970 const ObjCIvarDecl *OIVD) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001971 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001972 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001973 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1974 if (OIVD == Ivars[k])
1975 return OID;
1976 }
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001978 // Otherwise check in the super class.
1979 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1980 return FindIvarInterface(Context, Super, OIVD);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001982 return 0;
1983}
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001984
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001985llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001986 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001987 const ObjCIvarDecl *Ivar) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001988 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001989 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001990 return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
1991 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar"));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001992 }
Daniel Dunbar97776872009-04-22 07:32:20 +00001993 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001994 return llvm::ConstantInt::get(LongTy, Offset, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001995}
1996
Mike Stumpbb1c8602009-07-31 21:31:32 +00001997CodeGen::CGObjCRuntime *
1998CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) {
Chris Lattnerdce14062008-06-26 04:19:03 +00001999 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00002000}