blob: 3a0ac994d8cb5a14d285a1df195ff6587f3ded44 [file] [log] [blame]
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattnerb7256cd2008-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 Korobeynikov1200aca2008-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 Lattnerb7256cd2008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattner87ab27d2008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar97db84c2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattnerb6e9eb62009-05-08 00:11:50 +000020
Chris Lattner87ab27d2008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Chris Lattnerb6e9eb62009-05-08 00:11:50 +000026
27#include "llvm/Intrinsics.h"
Chris Lattnerb7256cd2008-03-01 08:50:34 +000028#include "llvm/Module.h"
Chris Lattnerb7256cd2008-03-01 08:50:34 +000029#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000030#include "llvm/ADT/StringMap.h"
Daniel Dunbar92992502008-08-15 22:20:32 +000031#include "llvm/Support/Compiler.h"
Daniel Dunbar92992502008-08-15 22:20:32 +000032#include "llvm/Target/TargetData.h"
Chris Lattnerb6e9eb62009-05-08 00:11:50 +000033
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000034#include <map>
Chris Lattner8d3f4a42009-01-27 05:06:01 +000035
36
Chris Lattner87ab27d2008-06-26 04:19:03 +000037using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000038using namespace CodeGen;
Anton Korobeynikov1200aca2008-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 Jahaniand20a03f2009-05-20 18:41:51 +000044static const int NonFragileRuntimeVersion = 9;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000045static const int ProtocolVersion = 2;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +000046static const int NonFragileProtocolVersion = 3;
Chris Lattnerb7256cd2008-03-01 08:50:34 +000047
Chris Lattnerb7256cd2008-03-01 08:50:34 +000048namespace {
Chris Lattner87ab27d2008-06-26 04:19:03 +000049class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattnerb7256cd2008-03-01 08:50:34 +000050private:
Chris Lattner87ab27d2008-06-26 04:19:03 +000051 CodeGen::CodeGenModule &CGM;
Chris Lattnerb7256cd2008-03-01 08:50:34 +000052 llvm::Module &TheModule;
Chris Lattner8d3f4a42009-01-27 05:06:01 +000053 const llvm::PointerType *SelectorTy;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +000054 const llvm::IntegerType *Int8Ty;
Chris Lattner8d3f4a42009-01-27 05:06:01 +000055 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +000056 const llvm::FunctionType *IMPTy;
Chris Lattner8d3f4a42009-01-27 05:06:01 +000057 const llvm::PointerType *IdTy;
David Chisnall9f57c292009-08-17 16:35:33 +000058 QualType ASTIdTy;
Chris Lattner8d3f4a42009-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 Dunbar566421c2009-05-04 15:31:17 +000063 llvm::GlobalAlias *ClassPtrAlias;
64 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000065 std::vector<llvm::Constant*> Classes;
66 std::vector<llvm::Constant*> Categories;
67 std::vector<llvm::Constant*> ConstantStrings;
David Chisnall358e7512010-01-27 12:49:23 +000068 llvm::StringMap<llvm::Constant*> ObjCStrings;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000069 llvm::Function *LoadFunction;
70 llvm::StringMap<llvm::Constant*> ExistingProtocols;
71 typedef std::pair<std::string, std::string> TypedSelector;
72 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
73 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
74 // Some zeros used for GEPs in lots of places.
75 llvm::Constant *Zeros[2];
76 llvm::Constant *NULLPtr;
Owen Anderson170229f2009-07-14 23:10:40 +000077 llvm::LLVMContext &VMContext;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000078private:
79 llvm::Constant *GenerateIvarList(
80 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
81 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
82 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
83 llvm::Constant *GenerateMethodList(const std::string &ClassName,
84 const std::string &CategoryName,
Mike Stump11289f42009-09-09 15:08:12 +000085 const llvm::SmallVectorImpl<Selector> &MethodSels,
86 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000087 bool isClassMethodList);
Fariborz Jahanian89d23972009-03-31 18:27:22 +000088 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +000089 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
90 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
91 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000092 llvm::Constant *GenerateProtocolList(
93 const llvm::SmallVectorImpl<std::string> &Protocols);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +000094 // To ensure that all protocols are seen by the runtime, we add a category on
95 // a class defined in the runtime, declaring no methods, but adopting the
96 // protocols.
97 void GenerateProtocolHolderCategory(void);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000098 llvm::Constant *GenerateClassStructure(
99 llvm::Constant *MetaClass,
100 llvm::Constant *SuperClass,
101 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000102 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000103 llvm::Constant *Version,
104 llvm::Constant *InstanceSize,
105 llvm::Constant *IVars,
106 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000107 llvm::Constant *Protocols,
108 llvm::Constant *IvarOffsets,
109 llvm::Constant *Properties);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000110 llvm::Constant *GenerateProtocolMethodList(
111 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
112 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
113 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
114 &Name="");
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000115 llvm::Constant *ExportUniqueString(const std::string &Str, const std::string
116 prefix);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000117 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000118 std::vector<llvm::Constant*> &V, const std::string &Name="",
119 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000120 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000121 std::vector<llvm::Constant*> &V, const std::string &Name="",
122 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +0000123 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
124 const ObjCIvarDecl *Ivar);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000125 void EmitClassRef(const std::string &className);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000126public:
Chris Lattner87ab27d2008-06-26 04:19:03 +0000127 CGObjCGNU(CodeGen::CodeGenModule &cgm);
David Chisnall481e3a82010-01-23 02:40:42 +0000128 virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
Mike Stump11289f42009-09-09 15:08:12 +0000129 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000130 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000131 QualType ResultType,
132 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000133 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000134 bool IsClassMessage,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000135 const CallArgList &CallArgs,
136 const ObjCMethodDecl *Method);
Mike Stump11289f42009-09-09 15:08:12 +0000137 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000138 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000139 QualType ResultType,
140 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000141 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000142 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000143 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000144 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000145 const CallArgList &CallArgs,
146 const ObjCMethodDecl *Method);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000147 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000148 const ObjCInterfaceDecl *OID);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000149 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000150 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
151 *Method);
Mike Stump11289f42009-09-09 15:08:12 +0000152
153 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000154 const ObjCContainerDecl *CD);
Daniel Dunbar92992502008-08-15 22:20:32 +0000155 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
156 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000157 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000158 const ObjCProtocolDecl *PD);
159 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000160 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbara91c3e02008-09-24 03:38:44 +0000161 virtual llvm::Function *GetPropertyGetFunction();
162 virtual llvm::Function *GetPropertySetFunction();
Daniel Dunbarc46a0792009-07-24 07:40:24 +0000163 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000164
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000165 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
166 const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000167 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
168 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000169 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000170 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000171 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
172 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000173 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
174 llvm::Value *src, llvm::Value *dest);
Fariborz Jahaniane881b532008-11-20 19:23:36 +0000175 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000176 llvm::Value *src, llvm::Value *dest,
177 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000178 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
179 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000180 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +0000181 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000182 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000183 QualType Ty);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000184 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
185 QualType ObjectTy,
186 llvm::Value *BaseValue,
187 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000188 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000189 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +0000190 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000191 const ObjCIvarDecl *Ivar);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000192};
193} // end anonymous namespace
194
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000195
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000196/// Emits a reference to a dummy variable which is emitted with each class.
197/// This ensures that a linker error will be generated when trying to link
198/// together modules where a referenced class is not defined.
Mike Stumpdd93a192009-07-31 21:31:32 +0000199void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000200 std::string symbolRef = "__objc_class_ref_" + className;
201 // Don't emit two copies of the same symbol
Mike Stumpdd93a192009-07-31 21:31:32 +0000202 if (TheModule.getGlobalVariable(symbolRef))
203 return;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000204 std::string symbolName = "__objc_class_name_" + className;
205 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
206 if (!ClassSymbol) {
Owen Andersonc10c8d32009-07-08 19:05:04 +0000207 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
208 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000209 }
Owen Andersonc10c8d32009-07-08 19:05:04 +0000210 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerc58e5692009-08-05 05:25:18 +0000211 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000212}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000213
214static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000215 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000216}
217
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000218static std::string SymbolNameForMethod(const std::string &ClassName, const
219 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
220{
David Chisnall035ead22010-01-14 14:08:19 +0000221 std::string MethodNameColonStripped = MethodName;
222 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
223 ':', '_');
224 return std::string(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
225 CategoryName + "_" + MethodNameColonStripped;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000226}
227
Chris Lattner87ab27d2008-06-26 04:19:03 +0000228CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar566421c2009-05-04 15:31:17 +0000229 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Anderson170229f2009-07-14 23:10:40 +0000230 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000231 IntTy = cast<llvm::IntegerType>(
232 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
233 LongTy = cast<llvm::IntegerType>(
234 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000235
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000236 Int8Ty = llvm::Type::getInt8Ty(VMContext);
237 // C string type. Used in lots of places.
238 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
239
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000240 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000241 Zeros[1] = Zeros[0];
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000242 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner4bd55962008-03-30 23:03:07 +0000243 // Get the selector Type.
David Chisnall481e3a82010-01-23 02:40:42 +0000244 QualType selTy = CGM.getContext().getObjCSelType();
245 if (QualType() == selTy) {
246 SelectorTy = PtrToInt8Ty;
247 } else {
248 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
249 }
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000250
Owen Anderson9793f0e2009-07-29 22:16:19 +0000251 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner4bd55962008-03-30 23:03:07 +0000252 PtrTy = PtrToInt8Ty;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Chris Lattner4bd55962008-03-30 23:03:07 +0000254 // Object type
David Chisnall9f57c292009-08-17 16:35:33 +0000255 ASTIdTy = CGM.getContext().getObjCIdType();
David Chisnall481e3a82010-01-23 02:40:42 +0000256 if (QualType() == ASTIdTy) {
257 IdTy = PtrToInt8Ty;
258 } else {
259 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
260 }
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattner4bd55962008-03-30 23:03:07 +0000262 // IMP type
263 std::vector<const llvm::Type*> IMPArgs;
264 IMPArgs.push_back(IdTy);
265 IMPArgs.push_back(SelectorTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000266 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000267}
Mike Stumpdd93a192009-07-31 21:31:32 +0000268
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000269// This has to perform the lookup every time, since posing and related
270// techniques can modify the name -> class mapping.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000271llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000272 const ObjCInterfaceDecl *OID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000273 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
David Chisnalldf349172010-01-08 00:14:31 +0000274 // With the incompatible ABI, this will need to be replaced with a direct
275 // reference to the class symbol. For the compatible nonfragile ABI we are
276 // still performing this lookup at run time but emitting the symbol for the
277 // class externally so that we can make the switch later.
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000278 EmitClassRef(OID->getNameAsString());
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000279 ClassName = Builder.CreateStructGEP(ClassName, 0);
280
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000281 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000282 llvm::Constant *ClassLookupFn =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000283 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000284 Params,
285 true),
286 "objc_lookup_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000287 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner4bd55962008-03-30 23:03:07 +0000288}
289
Daniel Dunbarcb463852008-11-01 01:53:16 +0000290llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnere4b95692008-11-24 03:33:13 +0000291 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner6d522c02008-06-26 04:37:12 +0000292 if (US == 0)
Owen Anderson9793f0e2009-07-29 22:16:19 +0000293 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
David Chisnall9f57c292009-08-17 16:35:33 +0000294 llvm::GlobalValue::PrivateLinkage,
295 ".objc_untyped_selector_alias"+Sel.getAsString(),
Chris Lattner6d522c02008-06-26 04:37:12 +0000296 NULL, &TheModule);
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner6d522c02008-06-26 04:37:12 +0000298 return Builder.CreateLoad(US);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000299}
300
301llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
302 *Method) {
303
304 std::string SelName = Method->getSelector().getAsString();
305 std::string SelTypes;
306 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
307 // Typed selectors
308 TypedSelector Selector = TypedSelector(SelName,
309 SelTypes);
310
311 // If it's already cached, return it.
Mike Stumpdd93a192009-07-31 21:31:32 +0000312 if (TypedSelectors[Selector]) {
313 return Builder.CreateLoad(TypedSelectors[Selector]);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000314 }
315
316 // If it isn't, cache it.
317 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Owen Anderson9793f0e2009-07-29 22:16:19 +0000318 llvm::PointerType::getUnqual(SelectorTy),
David Chisnall9f57c292009-08-17 16:35:33 +0000319 llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000320 NULL, &TheModule);
321 TypedSelectors[Selector] = Sel;
322
323 return Builder.CreateLoad(Sel);
Chris Lattner6d522c02008-06-26 04:37:12 +0000324}
325
Chris Lattnerd9b98862008-06-26 04:44:19 +0000326llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
327 const std::string &Name) {
David Chisnall5778fce2009-08-31 16:41:57 +0000328 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Owen Andersonade90fd2009-07-29 18:54:39 +0000329 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000330}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000331llvm::Constant *CGObjCGNU::ExportUniqueString(const std::string &Str,
332 const std::string prefix) {
333 std::string name = prefix + Str;
334 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
335 if (!ConstStr) {
336 llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
337 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
338 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
339 }
340 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
341}
Mike Stumpdd93a192009-07-31 21:31:32 +0000342
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000343llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000344 std::vector<llvm::Constant*> &V, const std::string &Name,
345 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson0e0189d2009-07-27 22:29:56 +0000346 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000347 return new llvm::GlobalVariable(TheModule, Ty, false,
348 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000349}
Mike Stumpdd93a192009-07-31 21:31:32 +0000350
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000351llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000352 std::vector<llvm::Constant*> &V, const std::string &Name,
353 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson47034e12009-07-28 18:33:04 +0000354 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000355 return new llvm::GlobalVariable(TheModule, Ty, false,
Mike Stumpdd93a192009-07-31 21:31:32 +0000356 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000357}
358
359/// Generate an NSConstantString object.
David Chisnall481e3a82010-01-23 02:40:42 +0000360llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall358e7512010-01-27 12:49:23 +0000361
David Chisnall481e3a82010-01-23 02:40:42 +0000362 std::string Str(SL->getStrData(), SL->getByteLength());
363
David Chisnall358e7512010-01-27 12:49:23 +0000364 // Look for an existing one
365 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
366 if (old != ObjCStrings.end())
367 return old->getValue();
368
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000369 std::vector<llvm::Constant*> Ivars;
370 Ivars.push_back(NULLPtr);
Chris Lattner091f6982008-06-21 21:44:18 +0000371 Ivars.push_back(MakeConstantString(Str));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000372 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000373 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson758428f2009-08-05 23:18:46 +0000374 llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000375 Ivars, ".objc_str");
David Chisnall358e7512010-01-27 12:49:23 +0000376 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
377 ObjCStrings[Str] = ObjCStr;
378 ConstantStrings.push_back(ObjCStr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000379 return ObjCStr;
380}
381
382///Generates a message send where the super is the receiver. This is a message
383///send to self with special delivery semantics indicating which class's method
384///should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000385CodeGen::RValue
386CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000387 QualType ResultType,
388 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000389 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000390 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000391 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000392 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000393 const CallArgList &CallArgs,
394 const ObjCMethodDecl *Method) {
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000395 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
396
397 CallArgList ActualArgs;
398
399 ActualArgs.push_back(
David Chisnall9f57c292009-08-17 16:35:33 +0000400 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
401 ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000402 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
403 CGF.getContext().getObjCSelType()));
404 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
405
406 CodeGenTypes &Types = CGM.getTypes();
407 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000408 const llvm::FunctionType *impType =
409 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000410
Daniel Dunbar566421c2009-05-04 15:31:17 +0000411 llvm::Value *ReceiverClass = 0;
Chris Lattnera02cb802009-05-08 15:39:58 +0000412 if (isCategoryImpl) {
413 llvm::Constant *classLookupFunction = 0;
414 std::vector<const llvm::Type*> Params;
415 Params.push_back(PtrTy);
416 if (IsClassMessage) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000417 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000418 IdTy, Params, true), "objc_get_meta_class");
419 } else {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000420 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000421 IdTy, Params, true), "objc_get_class");
Daniel Dunbar566421c2009-05-04 15:31:17 +0000422 }
Chris Lattnera02cb802009-05-08 15:39:58 +0000423 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
424 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000425 } else {
Chris Lattnera02cb802009-05-08 15:39:58 +0000426 // Set up global aliases for the metaclass or class pointer if they do not
427 // already exist. These will are forward-references which will be set to
Mike Stumpdd93a192009-07-31 21:31:32 +0000428 // pointers to the class and metaclass structure created for the runtime
429 // load function. To send a message to super, we look up the value of the
Chris Lattnera02cb802009-05-08 15:39:58 +0000430 // super_class pointer from either the class or metaclass structure.
431 if (IsClassMessage) {
432 if (!MetaClassPtrAlias) {
433 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
434 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
435 Class->getNameAsString(), NULL, &TheModule);
436 }
437 ReceiverClass = MetaClassPtrAlias;
438 } else {
439 if (!ClassPtrAlias) {
440 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
441 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
442 Class->getNameAsString(), NULL, &TheModule);
443 }
444 ReceiverClass = ClassPtrAlias;
Daniel Dunbar566421c2009-05-04 15:31:17 +0000445 }
Chris Lattnerc06ce0f2009-04-25 23:19:45 +0000446 }
Daniel Dunbar566421c2009-05-04 15:31:17 +0000447 // Cast the pointer to a simplified version of the class structure
Mike Stump11289f42009-09-09 15:08:12 +0000448 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000449 llvm::PointerType::getUnqual(
Owen Anderson758428f2009-08-05 23:18:46 +0000450 llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000451 // Get the superclass pointer
452 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
453 // Load the superclass pointer
454 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000455 // Construct the structure used to look up the IMP
Owen Anderson758428f2009-08-05 23:18:46 +0000456 llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
457 Receiver->getType(), IdTy, NULL);
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000458 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000459
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000460 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000461 CGF.Builder.CreateStore(ReceiverClass,
462 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000463
464 // Get the IMP
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000465 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000466 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000467 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000468 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000469 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
470 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000471 "objc_msg_lookup_super");
472
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000473 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000474 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000475 lookupArgs+2);
476
Anders Carlsson61a401c2009-12-24 19:25:24 +0000477 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000478}
479
Mike Stump11289f42009-09-09 15:08:12 +0000480/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000481CodeGen::RValue
482CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000483 QualType ResultType,
484 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000485 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000486 bool IsClassMessage,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000487 const CallArgList &CallArgs,
488 const ObjCMethodDecl *Method) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000489 CGBuilderTy &Builder = CGF.Builder;
David Chisnall9f57c292009-08-17 16:35:33 +0000490 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000491 llvm::Value *cmd;
492 if (Method)
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000493 cmd = GetSelector(Builder, Method);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000494 else
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000495 cmd = GetSelector(Builder, Sel);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000496 CallArgList ActualArgs;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000497
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000498 Receiver = Builder.CreateBitCast(Receiver, IdTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000499 ActualArgs.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000500 std::make_pair(RValue::get(Receiver), ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000501 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
502 CGF.getContext().getObjCSelType()));
503 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
504
505 CodeGenTypes &Types = CGM.getTypes();
506 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000507 const llvm::FunctionType *impType =
508 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000509
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000510 llvm::Value *imp;
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000511 // For sender-aware dispatch, we pass the sender as the third argument to a
512 // lookup function. When sending messages from C code, the sender is nil.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000513 // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
514 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
515
516 std::vector<const llvm::Type*> Params;
517 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
518 Builder.CreateStore(Receiver, ReceiverPtr);
519 Params.push_back(ReceiverPtr->getType());
520 Params.push_back(SelectorTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000521 llvm::Value *self;
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000522
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000523 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
524 self = CGF.LoadObjCSelf();
525 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +0000526 self = llvm::ConstantPointerNull::get(IdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000527 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000528
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000529 Params.push_back(self->getType());
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000530
531 // The lookup function returns a slot, which can be safely cached.
532 llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy,
533 IntTy, llvm::PointerType::getUnqual(impType), NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000534 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000535 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000536 llvm::PointerType::getUnqual(SlotTy), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000537 "objc_msg_lookup_sender");
538
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000539 // The lookup function is guaranteed not to capture the receiver pointer.
540 if (llvm::Function *LookupFn = dyn_cast<llvm::Function>(lookupFunction)) {
541 LookupFn->setDoesNotCapture(1);
542 }
543
544 llvm::Value *slot =
545 Builder.CreateCall3(lookupFunction, ReceiverPtr, cmd, self);
546 imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
547 // The lookup function may have changed the receiver, so make sure we use
548 // the new one.
549 ActualArgs[0] =
550 std::make_pair(RValue::get(Builder.CreateLoad(ReceiverPtr)), ASTIdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000551 } else {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000552 std::vector<const llvm::Type*> Params;
553 Params.push_back(Receiver->getType());
554 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000555 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000556 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
557 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000558 "objc_msg_lookup");
559
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000560 imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000561 }
Chris Lattnerdbcc2ca2008-05-06 00:56:42 +0000562
Anders Carlsson61a401c2009-12-24 19:25:24 +0000563 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000564}
565
Mike Stump11289f42009-09-09 15:08:12 +0000566/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000567/// objc_category structures.
568llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Mike Stump11289f42009-09-09 15:08:12 +0000569 const std::string &CategoryName,
570 const llvm::SmallVectorImpl<Selector> &MethodSels,
571 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000572 bool isClassMethodList) {
David Chisnall9f57c292009-08-17 16:35:33 +0000573 if (MethodSels.empty())
574 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000575 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000576 llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000577 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
578 PtrToInt8Ty, // Method types
Owen Anderson9793f0e2009-07-29 22:16:19 +0000579 llvm::PointerType::getUnqual(IMPTy), //Method pointer
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000580 NULL);
581 std::vector<llvm::Constant*> Methods;
582 std::vector<llvm::Constant*> Elements;
583 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
584 Elements.clear();
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000585 if (llvm::Constant *Method =
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000586 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnere4b95692008-11-24 03:33:13 +0000587 MethodSels[i].getAsString(),
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000588 isClassMethodList))) {
David Chisnall5778fce2009-08-31 16:41:57 +0000589 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
590 Elements.push_back(C);
591 Elements.push_back(MethodTypes[i]);
Owen Andersonade90fd2009-07-29 18:54:39 +0000592 Method = llvm::ConstantExpr::getBitCast(Method,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000593 llvm::PointerType::getUnqual(IMPTy));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000594 Elements.push_back(Method);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000595 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000596 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000597 }
598
599 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000600 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000601 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000602 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattner882034d2008-06-26 04:52:29 +0000603 Methods);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000604
605 // Structure containing list pointer, array and array count
606 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Andersonc36edfe2009-08-13 23:27:53 +0000607 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000608 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Owen Anderson758428f2009-08-05 23:18:46 +0000609 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
Mike Stump11289f42009-09-09 15:08:12 +0000610 NextPtrTy,
611 IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000612 ObjCMethodArrayTy,
613 NULL);
614 // Refine next pointer type to concrete type
615 llvm::cast<llvm::OpaqueType>(
616 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
617 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
618
619 Methods.clear();
Owen Anderson7ec07a52009-07-30 23:11:26 +0000620 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +0000621 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson41a75022009-08-13 21:57:51 +0000622 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000623 MethodTypes.size()));
624 Methods.push_back(MethodArray);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000626 // Create an instance of the structure
627 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
628}
629
630/// Generates an IvarList. Used in construction of a objc_class.
631llvm::Constant *CGObjCGNU::GenerateIvarList(
632 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
633 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
634 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
David Chisnallb3b44ce2009-11-16 19:05:54 +0000635 if (IvarNames.size() == 0)
636 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000637 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000638 llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000639 PtrToInt8Ty,
640 PtrToInt8Ty,
641 IntTy,
642 NULL);
643 std::vector<llvm::Constant*> Ivars;
644 std::vector<llvm::Constant*> Elements;
645 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
646 Elements.clear();
David Chisnall5778fce2009-08-31 16:41:57 +0000647 Elements.push_back(IvarNames[i]);
648 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000649 Elements.push_back(IvarOffsets[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000650 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000651 }
652
653 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000654 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000655 IvarNames.size());
656
Mike Stump11289f42009-09-09 15:08:12 +0000657
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000658 Elements.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000659 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson47034e12009-07-28 18:33:04 +0000660 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000661 // Structure containing array and array count
Owen Anderson758428f2009-08-05 23:18:46 +0000662 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000663 ObjCIvarArrayTy,
664 NULL);
665
666 // Create an instance of the structure
667 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
668}
669
670/// Generate a class structure
671llvm::Constant *CGObjCGNU::GenerateClassStructure(
672 llvm::Constant *MetaClass,
673 llvm::Constant *SuperClass,
674 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000675 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000676 llvm::Constant *Version,
677 llvm::Constant *InstanceSize,
678 llvm::Constant *IVars,
679 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000680 llvm::Constant *Protocols,
681 llvm::Constant *IvarOffsets,
682 llvm::Constant *Properties) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000683 // Set up the class structure
684 // Note: Several of these are char*s when they should be ids. This is
685 // because the runtime performs this translation on load.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000686 //
687 // Fields marked New ABI are part of the GNUstep runtime. We emit them
688 // anyway; the classes will still work with the GNU runtime, they will just
689 // be ignored.
Owen Anderson758428f2009-08-05 23:18:46 +0000690 llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000691 PtrToInt8Ty, // class_pointer
692 PtrToInt8Ty, // super_class
693 PtrToInt8Ty, // name
694 LongTy, // version
695 LongTy, // info
696 LongTy, // instance_size
697 IVars->getType(), // ivars
698 Methods->getType(), // methods
Mike Stump11289f42009-09-09 15:08:12 +0000699 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000700 PtrTy, // dtable
701 PtrTy, // subclass_list
702 PtrTy, // sibling_class
703 PtrTy, // protocols
704 PtrTy, // gc_object_type
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000705 // New ABI:
706 LongTy, // abi_version
707 IvarOffsets->getType(), // ivar_offsets
708 Properties->getType(), // properties
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000709 NULL);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000710 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000711 // Fill in the structure
712 std::vector<llvm::Constant*> Elements;
Owen Andersonade90fd2009-07-29 18:54:39 +0000713 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000714 Elements.push_back(SuperClass);
Chris Lattnerda35bc82008-06-26 04:47:04 +0000715 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000716 Elements.push_back(Zero);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000717 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000718 Elements.push_back(InstanceSize);
719 Elements.push_back(IVars);
720 Elements.push_back(Methods);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000721 Elements.push_back(NULLPtr);
722 Elements.push_back(NULLPtr);
723 Elements.push_back(NULLPtr);
Owen Andersonade90fd2009-07-29 18:54:39 +0000724 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000725 Elements.push_back(NULLPtr);
726 Elements.push_back(Zero);
727 Elements.push_back(IvarOffsets);
728 Elements.push_back(Properties);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000729 // Create an instance of the structure
David Chisnalldf349172010-01-08 00:14:31 +0000730 // This is now an externally visible symbol, so that we can speed up class
731 // messages in the next ABI.
732 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name),
733 llvm::GlobalValue::ExternalLinkage);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000734}
735
736llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
737 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
738 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
Mike Stump11289f42009-09-09 15:08:12 +0000739 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000740 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000741 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
742 PtrToInt8Ty,
743 NULL);
744 std::vector<llvm::Constant*> Methods;
745 std::vector<llvm::Constant*> Elements;
746 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
747 Elements.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000748 Elements.push_back(MethodNames[i]);
David Chisnall5778fce2009-08-31 16:41:57 +0000749 Elements.push_back(MethodTypes[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000750 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000751 }
Owen Anderson9793f0e2009-07-29 22:16:19 +0000752 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000753 MethodNames.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000754 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpdd93a192009-07-31 21:31:32 +0000755 Methods);
Owen Anderson758428f2009-08-05 23:18:46 +0000756 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000757 IntTy, ObjCMethodArrayTy, NULL);
758 Methods.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000759 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000760 Methods.push_back(Array);
761 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
762}
Mike Stumpdd93a192009-07-31 21:31:32 +0000763
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000764// Create the protocol list structure used in classes, categories and so on
765llvm::Constant *CGObjCGNU::GenerateProtocolList(
766 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000767 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000768 Protocols.size());
Owen Anderson758428f2009-08-05 23:18:46 +0000769 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000770 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
771 LongTy,//FIXME: Should be size_t
772 ProtocolArrayTy,
773 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000774 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000775 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
776 iter != endIter ; iter++) {
David Chisnallbc8bdea2009-11-20 14:50:59 +0000777 llvm::Constant *protocol = 0;
778 llvm::StringMap<llvm::Constant*>::iterator value =
779 ExistingProtocols.find(*iter);
780 if (value == ExistingProtocols.end()) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000781 protocol = GenerateEmptyProtocol(*iter);
David Chisnallbc8bdea2009-11-20 14:50:59 +0000782 } else {
783 protocol = value->getValue();
784 }
Owen Andersonade90fd2009-07-29 18:54:39 +0000785 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Anderson170229f2009-07-14 23:10:40 +0000786 PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000787 Elements.push_back(Ptr);
788 }
Owen Anderson47034e12009-07-28 18:33:04 +0000789 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000790 Elements);
791 Elements.clear();
792 Elements.push_back(NULLPtr);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000793 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000794 Elements.push_back(ProtocolArray);
795 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
796}
797
Mike Stump11289f42009-09-09 15:08:12 +0000798llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000799 const ObjCProtocolDecl *PD) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000800 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Mike Stump11289f42009-09-09 15:08:12 +0000801 const llvm::Type *T =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000802 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000803 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000804}
805
806llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
807 const std::string &ProtocolName) {
808 llvm::SmallVector<std::string, 0> EmptyStringVector;
809 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
810
811 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000812 llvm::Constant *MethodList =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000813 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
814 // Protocols are objects containing lists of the methods implemented and
815 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +0000816 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000817 PtrToInt8Ty,
818 ProtocolList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000819 MethodList->getType(),
820 MethodList->getType(),
821 MethodList->getType(),
822 MethodList->getType(),
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000823 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000824 std::vector<llvm::Constant*> Elements;
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000825 // The isa pointer must be set to a magic number so the runtime knows it's
826 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000827 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
828 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +0000829 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000830 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000831 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
832 Elements.push_back(ProtocolList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000833 Elements.push_back(MethodList);
834 Elements.push_back(MethodList);
835 Elements.push_back(MethodList);
836 Elements.push_back(MethodList);
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000837 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000838}
839
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000840void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
841 ASTContext &Context = CGM.getContext();
Chris Lattner86d7d912008-11-24 03:54:41 +0000842 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000843 llvm::SmallVector<std::string, 16> Protocols;
844 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
845 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000846 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000847 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
848 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000849 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
850 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000851 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
852 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000853 std::string TypeStr;
854 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000855 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
856 InstanceMethodNames.push_back(
857 MakeConstantString((*iter)->getSelector().getAsString()));
858 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
859 } else {
860 OptionalInstanceMethodNames.push_back(
861 MakeConstantString((*iter)->getSelector().getAsString()));
862 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
863 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000864 }
865 // Collect information about class methods:
866 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
867 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000868 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
869 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +0000870 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000871 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
872 iter != endIter ; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000873 std::string TypeStr;
874 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000875 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
876 ClassMethodNames.push_back(
877 MakeConstantString((*iter)->getSelector().getAsString()));
878 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
879 } else {
880 OptionalClassMethodNames.push_back(
881 MakeConstantString((*iter)->getSelector().getAsString()));
882 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
883 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000884 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000885
886 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
887 llvm::Constant *InstanceMethodList =
888 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
889 llvm::Constant *ClassMethodList =
890 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000891 llvm::Constant *OptionalInstanceMethodList =
892 GenerateProtocolMethodList(OptionalInstanceMethodNames,
893 OptionalInstanceMethodTypes);
894 llvm::Constant *OptionalClassMethodList =
895 GenerateProtocolMethodList(OptionalClassMethodNames,
896 OptionalClassMethodTypes);
897
898 // Property metadata: name, attributes, isSynthesized, setter name, setter
899 // types, getter name, getter types.
900 // The isSynthesized value is always set to 0 in a protocol. It exists to
901 // simplify the runtime library by allowing it to use the same data
902 // structures for protocol metadata everywhere.
903 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
904 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
905 PtrToInt8Ty, NULL);
906 std::vector<llvm::Constant*> Properties;
907 std::vector<llvm::Constant*> OptionalProperties;
908
909 // Add all of the property methods need adding to the method list and to the
910 // property metadata list.
911 for (ObjCContainerDecl::prop_iterator
912 iter = PD->prop_begin(), endIter = PD->prop_end();
913 iter != endIter ; iter++) {
914 std::vector<llvm::Constant*> Fields;
915 ObjCPropertyDecl *property = (*iter);
916
917 Fields.push_back(MakeConstantString(property->getNameAsString()));
918 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
919 property->getPropertyAttributes()));
920 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
921 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
922 std::string TypeStr;
923 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
924 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
925 InstanceMethodTypes.push_back(TypeEncoding);
926 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
927 Fields.push_back(TypeEncoding);
928 } else {
929 Fields.push_back(NULLPtr);
930 Fields.push_back(NULLPtr);
931 }
932 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
933 std::string TypeStr;
934 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
935 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
936 InstanceMethodTypes.push_back(TypeEncoding);
937 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
938 Fields.push_back(TypeEncoding);
939 } else {
940 Fields.push_back(NULLPtr);
941 Fields.push_back(NULLPtr);
942 }
943 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
944 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
945 } else {
946 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
947 }
948 }
949 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
950 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
951 llvm::Constant* PropertyListInitFields[] =
952 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
953
954 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000955 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000956 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
957 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
958 PropertyListInit, ".objc_property_list");
959
960 llvm::Constant *OptionalPropertyArray =
961 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
962 OptionalProperties.size()) , OptionalProperties);
963 llvm::Constant* OptionalPropertyListInitFields[] = {
964 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
965 OptionalPropertyArray };
966
967 llvm::Constant *OptionalPropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000968 llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000969 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
970 OptionalPropertyListInit->getType(), false,
971 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
972 ".objc_property_list");
973
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000974 // Protocols are objects containing lists of the methods implemented and
975 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +0000976 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000977 PtrToInt8Ty,
978 ProtocolList->getType(),
979 InstanceMethodList->getType(),
980 ClassMethodList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000981 OptionalInstanceMethodList->getType(),
982 OptionalClassMethodList->getType(),
983 PropertyList->getType(),
984 OptionalPropertyList->getType(),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000985 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000986 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000987 // The isa pointer must be set to a magic number so the runtime knows it's
988 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000989 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
990 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +0000991 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000992 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000993 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
994 Elements.push_back(ProtocolList);
995 Elements.push_back(InstanceMethodList);
996 Elements.push_back(ClassMethodList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000997 Elements.push_back(OptionalInstanceMethodList);
998 Elements.push_back(OptionalClassMethodList);
999 Elements.push_back(PropertyList);
1000 Elements.push_back(OptionalPropertyList);
Mike Stump11289f42009-09-09 15:08:12 +00001001 ExistingProtocols[ProtocolName] =
Owen Andersonade90fd2009-07-29 18:54:39 +00001002 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001003 ".objc_protocol"), IdTy);
1004}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001005void CGObjCGNU::GenerateProtocolHolderCategory(void) {
1006 // Collect information about instance methods
1007 llvm::SmallVector<Selector, 1> MethodSels;
1008 llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
1009
1010 std::vector<llvm::Constant*> Elements;
1011 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1012 const std::string CategoryName = "AnotherHack";
1013 Elements.push_back(MakeConstantString(CategoryName));
1014 Elements.push_back(MakeConstantString(ClassName));
1015 // Instance method list
1016 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1017 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1018 // Class method list
1019 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1020 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1021 // Protocol list
1022 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1023 ExistingProtocols.size());
1024 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
1025 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1026 LongTy,//FIXME: Should be size_t
1027 ProtocolArrayTy,
1028 NULL);
1029 std::vector<llvm::Constant*> ProtocolElements;
1030 for (llvm::StringMapIterator<llvm::Constant*> iter =
1031 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1032 iter != endIter ; iter++) {
1033 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1034 PtrTy);
1035 ProtocolElements.push_back(Ptr);
1036 }
1037 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1038 ProtocolElements);
1039 ProtocolElements.clear();
1040 ProtocolElements.push_back(NULLPtr);
1041 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1042 ExistingProtocols.size()));
1043 ProtocolElements.push_back(ProtocolArray);
1044 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1045 ProtocolElements, ".objc_protocol_list"), PtrTy));
1046 Categories.push_back(llvm::ConstantExpr::getBitCast(
1047 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
1048 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1049}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001050
Daniel Dunbar92992502008-08-15 22:20:32 +00001051void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001052 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1053 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar92992502008-08-15 22:20:32 +00001054 // Collect information about instance methods
1055 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1056 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001057 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001058 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001059 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001060 InstanceMethodSels.push_back((*iter)->getSelector());
1061 std::string TypeStr;
1062 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001063 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001064 }
1065
1066 // Collect information about class methods
1067 llvm::SmallVector<Selector, 16> ClassMethodSels;
1068 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001069 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001070 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001071 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001072 ClassMethodSels.push_back((*iter)->getSelector());
1073 std::string TypeStr;
1074 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001075 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001076 }
1077
1078 // Collect the names of referenced protocols
1079 llvm::SmallVector<std::string, 16> Protocols;
1080 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
1081 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1082 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1083 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001084 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001085
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001086 std::vector<llvm::Constant*> Elements;
1087 Elements.push_back(MakeConstantString(CategoryName));
1088 Elements.push_back(MakeConstantString(ClassName));
Mike Stump11289f42009-09-09 15:08:12 +00001089 // Instance method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001090 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001091 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001092 false), PtrTy));
1093 // Class method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001094 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001095 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001096 PtrTy));
1097 // Protocol list
Owen Andersonade90fd2009-07-29 18:54:39 +00001098 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001099 GenerateProtocolList(Protocols), PtrTy));
Owen Andersonade90fd2009-07-29 18:54:39 +00001100 Categories.push_back(llvm::ConstantExpr::getBitCast(
Mike Stump11289f42009-09-09 15:08:12 +00001101 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson758428f2009-08-05 23:18:46 +00001102 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001103}
Daniel Dunbar92992502008-08-15 22:20:32 +00001104
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001105llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1106 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1107 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1108 ASTContext &Context = CGM.getContext();
1109 //
1110 // Property metadata: name, attributes, isSynthesized, setter name, setter
1111 // types, getter name, getter types.
1112 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1113 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1114 PtrToInt8Ty, NULL);
1115 std::vector<llvm::Constant*> Properties;
1116
1117
1118 // Add all of the property methods need adding to the method list and to the
1119 // property metadata list.
1120 for (ObjCImplDecl::propimpl_iterator
1121 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1122 iter != endIter ; iter++) {
1123 std::vector<llvm::Constant*> Fields;
1124 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
1125
1126 Fields.push_back(MakeConstantString(property->getNameAsString()));
1127 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1128 property->getPropertyAttributes()));
1129 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1130 (*iter)->getPropertyImplementation() ==
1131 ObjCPropertyImplDecl::Synthesize));
1132 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1133 InstanceMethodSels.push_back(getter->getSelector());
1134 std::string TypeStr;
1135 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1136 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1137 InstanceMethodTypes.push_back(TypeEncoding);
1138 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1139 Fields.push_back(TypeEncoding);
1140 } else {
1141 Fields.push_back(NULLPtr);
1142 Fields.push_back(NULLPtr);
1143 }
1144 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1145 InstanceMethodSels.push_back(setter->getSelector());
1146 std::string TypeStr;
1147 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1148 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1149 InstanceMethodTypes.push_back(TypeEncoding);
1150 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1151 Fields.push_back(TypeEncoding);
1152 } else {
1153 Fields.push_back(NULLPtr);
1154 Fields.push_back(NULLPtr);
1155 }
1156 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1157 }
1158 llvm::ArrayType *PropertyArrayTy =
1159 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1160 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1161 Properties);
1162 llvm::Constant* PropertyListInitFields[] =
1163 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1164
1165 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001166 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001167 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1168 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1169 ".objc_property_list");
1170}
1171
Daniel Dunbar92992502008-08-15 22:20:32 +00001172void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1173 ASTContext &Context = CGM.getContext();
1174
1175 // Get the superclass name.
Mike Stump11289f42009-09-09 15:08:12 +00001176 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar92992502008-08-15 22:20:32 +00001177 OID->getClassInterface()->getSuperClass();
Chris Lattner86d7d912008-11-24 03:54:41 +00001178 std::string SuperClassName;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001179 if (SuperClassDecl) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001180 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001181 EmitClassRef(SuperClassName);
1182 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001183
1184 // Get the class name
Chris Lattner87bc3872009-04-01 02:00:48 +00001185 ObjCInterfaceDecl *ClassDecl =
1186 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner86d7d912008-11-24 03:54:41 +00001187 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001188 // Emit the symbol that is used to generate linker errors if this class is
1189 // referenced in other modules but not declared.
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001190 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump11289f42009-09-09 15:08:12 +00001191 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001192 TheModule.getGlobalVariable(classSymbolName)) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001193 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001194 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00001195 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001196 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Andersonc10c8d32009-07-08 19:05:04 +00001197 classSymbolName);
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001198 }
Mike Stump11289f42009-09-09 15:08:12 +00001199
Daniel Dunbar12119b92009-05-03 10:46:44 +00001200 // Get the size of instances.
1201 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar92992502008-08-15 22:20:32 +00001202
1203 // Collect information about instance variables.
1204 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
1205 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
1206 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001208 std::vector<llvm::Constant*> IvarOffsetValues;
1209
Mike Stump11289f42009-09-09 15:08:12 +00001210 int superInstanceSize = !SuperClassDecl ? 0 :
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001211 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
1212 // For non-fragile ivars, set the instance size to 0 - {the size of just this
1213 // class}. The runtime will then set this to the correct value on load.
1214 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1215 instanceSize = 0 - (instanceSize - superInstanceSize);
1216 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001217 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1218 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1219 // Store the name
David Chisnall5778fce2009-08-31 16:41:57 +00001220 IvarNames.push_back(MakeConstantString((*iter)->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001221 // Get the type encoding for this ivar
1222 std::string TypeStr;
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00001223 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001224 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001225 // Get the offset
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001226 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
David Chisnallcb1b7bf2009-11-17 19:32:15 +00001227 uint64_t Offset = BaseOffset;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001228 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001229 Offset = BaseOffset - superInstanceSize;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001230 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001231 IvarOffsets.push_back(
Owen Anderson41a75022009-08-13 21:57:51 +00001232 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001233 IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1234 false, llvm::GlobalValue::ExternalLinkage,
1235 llvm::ConstantInt::get(IntTy, BaseOffset),
1236 "__objc_ivar_offset_value_" + ClassName +"." +
1237 (*iter)->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001238 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001239 llvm::Constant *IvarOffsetArrayInit =
1240 llvm::ConstantArray::get(llvm::ArrayType::get(PtrToIntTy,
1241 IvarOffsetValues.size()), IvarOffsetValues);
1242 llvm::GlobalVariable *IvarOffsetArray = new llvm::GlobalVariable(TheModule,
1243 IvarOffsetArrayInit->getType(), false,
1244 llvm::GlobalValue::InternalLinkage, IvarOffsetArrayInit,
1245 ".ivar.offsets");
Daniel Dunbar92992502008-08-15 22:20:32 +00001246
1247 // Collect information about instance methods
1248 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1249 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001250 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001251 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001252 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001253 InstanceMethodSels.push_back((*iter)->getSelector());
1254 std::string TypeStr;
1255 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001256 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001257 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001258
1259 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1260 InstanceMethodTypes);
1261
Daniel Dunbar92992502008-08-15 22:20:32 +00001262
1263 // Collect information about class methods
1264 llvm::SmallVector<Selector, 16> ClassMethodSels;
1265 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001266 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001267 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001268 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001269 ClassMethodSels.push_back((*iter)->getSelector());
1270 std::string TypeStr;
1271 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001272 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001273 }
1274 // Collect the names of referenced protocols
1275 llvm::SmallVector<std::string, 16> Protocols;
1276 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1277 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1278 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001279 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001280
1281
1282
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001283 // Get the superclass pointer.
1284 llvm::Constant *SuperClass;
Chris Lattner86d7d912008-11-24 03:54:41 +00001285 if (!SuperClassName.empty()) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001286 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
1287 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +00001288 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001289 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001290 // Empty vector used to construct empty method lists
1291 llvm::SmallVector<llvm::Constant*, 1> empty;
1292 // Generate the method and instance variable lists
1293 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001294 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001295 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001296 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001297 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
1298 IvarOffsets);
Mike Stump11289f42009-09-09 15:08:12 +00001299 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall5778fce2009-08-31 16:41:57 +00001300 // we emit a symbol containing the offset for each ivar in the class. This
1301 // allows code compiled for the non-Fragile ABI to inherit from code compiled
1302 // for the legacy ABI, without causing problems. The converse is also
1303 // possible, but causes all ivar accesses to be fragile.
1304 int i = 0;
1305 // Offset pointer for getting at the correct field in the ivar list when
1306 // setting up the alias. These are: The base address for the global, the
1307 // ivar array (second field), the ivar in this list (set for each ivar), and
1308 // the offset (third field in ivar structure)
1309 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
1310 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump11289f42009-09-09 15:08:12 +00001311 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall5778fce2009-08-31 16:41:57 +00001312 llvm::ConstantInt::get(IndexTy, 2) };
1313
1314 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1315 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1316 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1317 +(*iter)->getNameAsString();
1318 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++);
1319 // Get the correct ivar field
1320 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1321 IvarList, offsetPointerIndexes, 4);
1322 // Get the existing alias, if one exists.
1323 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1324 if (offset) {
1325 offset->setInitializer(offsetValue);
1326 // If this is the real definition, change its linkage type so that
1327 // different modules will use this one, rather than their private
1328 // copy.
1329 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1330 } else {
1331 // Add a new alias if there isn't one already.
1332 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1333 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1334 }
1335 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001336 //Generate metaclass for class methods
1337 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnallb3b44ce2009-11-16 19:05:54 +00001338 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001339 empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001340
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001341 // Generate the class structure
Chris Lattner86d7d912008-11-24 03:54:41 +00001342 llvm::Constant *ClassStruct =
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001343 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner86d7d912008-11-24 03:54:41 +00001344 ClassName.c_str(), 0,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001345 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001346 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1347 Properties);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001348
1349 // Resolve the class aliases, if they exist.
1350 if (ClassPtrAlias) {
1351 ClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001352 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001353 ClassPtrAlias = 0;
1354 }
1355 if (MetaClassPtrAlias) {
1356 MetaClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001357 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001358 MetaClassPtrAlias = 0;
1359 }
1360
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001361 // Add class structure to list to be added to the symtab later
Owen Andersonade90fd2009-07-29 18:54:39 +00001362 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001363 Classes.push_back(ClassStruct);
1364}
1365
Fariborz Jahanian248c7192009-06-23 21:47:46 +00001366
Mike Stump11289f42009-09-09 15:08:12 +00001367llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001368 // Only emit an ObjC load function if no Objective-C stuff has been called
1369 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1370 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov3b6dd582008-06-01 15:14:46 +00001371 UntypedSelectors.empty())
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001372 return NULL;
Eli Friedman412c6682008-06-01 16:00:02 +00001373
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001374 // Add all referenced protocols to a category.
1375 GenerateProtocolHolderCategory();
1376
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001377 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1378 SelectorTy->getElementType());
1379 const llvm::Type *SelStructPtrTy = SelectorTy;
1380 bool isSelOpaque = false;
1381 if (SelStructTy == 0) {
Owen Anderson758428f2009-08-05 23:18:46 +00001382 SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
1383 PtrToInt8Ty, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001384 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001385 isSelOpaque = true;
1386 }
1387
Eli Friedman412c6682008-06-01 16:00:02 +00001388 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001389 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman412c6682008-06-01 16:00:02 +00001390 TheModule.addTypeName(".objc_id", IdTy);
1391 TheModule.addTypeName(".objc_imp", IMPTy);
1392
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001393 std::vector<llvm::Constant*> Elements;
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001394 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001395 // Generate statics list:
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001396 if (ConstantStrings.size()) {
Owen Anderson9793f0e2009-07-29 22:16:19 +00001397 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001398 ConstantStrings.size() + 1);
1399 ConstantStrings.push_back(NULLPtr);
David Chisnall5778fce2009-08-31 16:41:57 +00001400
Daniel Dunbar75fa84e2009-11-29 02:38:47 +00001401 llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
1402 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall5778fce2009-08-31 16:41:57 +00001403 Elements.push_back(MakeConstantString(StringClass,
1404 ".objc_static_class_name"));
Owen Anderson47034e12009-07-28 18:33:04 +00001405 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001406 ConstantStrings));
Mike Stump11289f42009-09-09 15:08:12 +00001407 llvm::StructType *StaticsListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00001408 llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Anderson170229f2009-07-14 23:10:40 +00001409 llvm::Type *StaticsListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001410 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001411 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump11289f42009-09-09 15:08:12 +00001412 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001413 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001414 Elements.clear();
1415 Elements.push_back(Statics);
Owen Anderson0b75f232009-07-31 20:28:54 +00001416 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001417 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Andersonade90fd2009-07-29 18:54:39 +00001418 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001419 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001420 // Array of classes, categories, and constant objects
Owen Anderson9793f0e2009-07-29 22:16:19 +00001421 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001422 Classes.size() + Categories.size() + 2);
Mike Stump11289f42009-09-09 15:08:12 +00001423 llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00001424 LongTy, SelStructPtrTy,
Owen Anderson41a75022009-08-13 21:57:51 +00001425 llvm::Type::getInt16Ty(VMContext),
1426 llvm::Type::getInt16Ty(VMContext),
Chris Lattner63dd3372008-06-26 04:10:42 +00001427 ClassListTy, NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001428
1429 Elements.clear();
1430 // Pointer to an array of selectors used in this module.
1431 std::vector<llvm::Constant*> Selectors;
1432 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1433 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1434 iter != iterEnd ; ++iter) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001435 Elements.push_back(ExportUniqueString(iter->first.first, ".objc_sel_name"));
David Chisnall29979822009-09-14 19:04:10 +00001436 Elements.push_back(MakeConstantString(iter->first.second,
Chris Lattner63dd3372008-06-26 04:10:42 +00001437 ".objc_sel_types"));
Owen Anderson0e0189d2009-07-27 22:29:56 +00001438 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001439 Elements.clear();
1440 }
1441 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1442 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner63dd3372008-06-26 04:10:42 +00001443 iter != iterEnd; ++iter) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001444 Elements.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001445 ExportUniqueString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001446 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001447 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001448 Elements.clear();
1449 }
1450 Elements.push_back(NULLPtr);
1451 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001452 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001453 Elements.clear();
1454 // Number of static selectors
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001455 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001456 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson9793f0e2009-07-29 22:16:19 +00001457 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001458 ".objc_selector_list");
Mike Stump11289f42009-09-09 15:08:12 +00001459 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001460 SelStructPtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001461
1462 // Now that all of the static selectors exist, create pointers to them.
1463 int index = 0;
1464 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1465 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1466 iter != iterEnd; ++iter) {
1467 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001468 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Owen Andersonc10c8d32009-07-08 19:05:04 +00001469 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1470 true, llvm::GlobalValue::InternalLinkage,
Owen Andersonade90fd2009-07-29 18:54:39 +00001471 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
Owen Andersonc10c8d32009-07-08 19:05:04 +00001472 ".objc_sel_ptr");
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001473 // If selectors are defined as an opaque type, cast the pointer to this
1474 // type.
1475 if (isSelOpaque) {
Owen Andersonade90fd2009-07-29 18:54:39 +00001476 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001477 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001478 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001479 (*iter).second->setAliasee(SelPtr);
1480 }
1481 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1482 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1483 iter != iterEnd; iter++) {
1484 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001485 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Mike Stumpdd93a192009-07-31 21:31:32 +00001486 llvm::Constant *SelPtr = new llvm::GlobalVariable
Mike Stump11289f42009-09-09 15:08:12 +00001487 (TheModule, SelStructPtrTy,
Mike Stumpdd93a192009-07-31 21:31:32 +00001488 true, llvm::GlobalValue::InternalLinkage,
1489 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1490 ".objc_sel_ptr");
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001491 // If selectors are defined as an opaque type, cast the pointer to this
1492 // type.
1493 if (isSelOpaque) {
Owen Andersonade90fd2009-07-29 18:54:39 +00001494 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001495 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001496 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001497 (*iter).second->setAliasee(SelPtr);
1498 }
1499 // Number of classes defined.
Mike Stump11289f42009-09-09 15:08:12 +00001500 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001501 Classes.size()));
1502 // Number of categories defined
Mike Stump11289f42009-09-09 15:08:12 +00001503 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001504 Categories.size()));
1505 // Create an array of classes, then categories, then static object instances
1506 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1507 // NULL-terminated list of static object instances (mainly constant strings)
1508 Classes.push_back(Statics);
1509 Classes.push_back(NULLPtr);
Owen Anderson47034e12009-07-28 18:33:04 +00001510 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001511 Elements.push_back(ClassList);
Mike Stump11289f42009-09-09 15:08:12 +00001512 // Construct the symbol table
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001513 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1514
1515 // The symbol table is contained in a module which has some version-checking
1516 // constants
Owen Anderson758428f2009-08-05 23:18:46 +00001517 llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001518 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001519 Elements.clear();
1520 // Runtime version used for compatibility checking.
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001521 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Mike Stump11289f42009-09-09 15:08:12 +00001522 Elements.push_back(llvm::ConstantInt::get(LongTy,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001523 NonFragileRuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001524 } else {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001525 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001526 }
Fariborz Jahanianc2d56182009-04-01 19:49:42 +00001527 // sizeof(ModuleTy)
1528 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001529 Elements.push_back(llvm::ConstantInt::get(LongTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001530 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001531 //FIXME: Should be the path to the file where this module was declared
1532 Elements.push_back(NULLPtr);
1533 Elements.push_back(SymTab);
1534 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1535
1536 // Create the load function calling the runtime entry point with the module
1537 // structure
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001538 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson41a75022009-08-13 21:57:51 +00001539 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001540 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1541 &TheModule);
Owen Anderson41a75022009-08-13 21:57:51 +00001542 llvm::BasicBlock *EntryBB =
1543 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Anderson170229f2009-07-14 23:10:40 +00001544 CGBuilderTy Builder(VMContext);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001545 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian3b636c12009-03-30 18:02:14 +00001546
1547 std::vector<const llvm::Type*> Params(1,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001548 llvm::PointerType::getUnqual(ModuleTy));
1549 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +00001550 llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001551 Builder.CreateCall(Register, Module);
1552 Builder.CreateRetVoid();
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001553
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001554 return LoadFunction;
1555}
Daniel Dunbar92992502008-08-15 22:20:32 +00001556
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00001557llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump11289f42009-09-09 15:08:12 +00001558 const ObjCContainerDecl *CD) {
1559 const ObjCCategoryImplDecl *OCD =
Steve Naroff11b387f2009-01-08 19:41:02 +00001560 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattnere4b95692008-11-24 03:33:13 +00001561 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1562 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1563 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorffca3a22009-01-09 17:18:27 +00001564 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar92992502008-08-15 22:20:32 +00001565
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001566 CodeGenTypes &Types = CGM.getTypes();
Mike Stump11289f42009-09-09 15:08:12 +00001567 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001568 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001569 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1570 MethodName, isClassMethod);
1571
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001572 llvm::Function *Method
Mike Stump11289f42009-09-09 15:08:12 +00001573 = llvm::Function::Create(MethodTy,
1574 llvm::GlobalValue::InternalLinkage,
1575 FunctionName,
1576 &TheModule);
Chris Lattner4bd55962008-03-30 23:03:07 +00001577 return Method;
1578}
1579
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001580llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Mike Stump11289f42009-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(BoolTy);
1589 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1590 const llvm::FunctionType *FTy =
1591 llvm::FunctionType::get(IdTy, Params, false);
1592 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1593 "objc_getProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001594}
1595
1596llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Mike Stump11289f42009-09-09 15:08:12 +00001597 std::vector<const llvm::Type*> Params;
1598 const llvm::Type *BoolTy =
1599 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1600 Params.push_back(IdTy);
1601 Params.push_back(SelectorTy);
1602 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1603 Params.push_back(LongTy);
1604 Params.push_back(IdTy);
1605 Params.push_back(BoolTy);
1606 Params.push_back(BoolTy);
1607 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1608 const llvm::FunctionType *FTy =
1609 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
1610 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1611 "objc_setProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001612}
1613
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001614llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
1615 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1616 ASTContext &Ctx = CGM.getContext();
1617 // void objc_enumerationMutation (id)
1618 llvm::SmallVector<QualType,16> Params;
David Chisnall9f57c292009-08-17 16:35:33 +00001619 Params.push_back(ASTIdTy);
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001620 const llvm::FunctionType *FTy =
1621 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
1622 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
Anders Carlsson3f35a262008-08-31 04:05:03 +00001623}
1624
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00001625void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1626 const Stmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001627 // Pointer to the personality function
1628 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +00001629 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +00001630 true),
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001631 "__gnu_objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +00001632 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001633 std::vector<const llvm::Type*> Params;
1634 Params.push_back(PtrTy);
1635 llvm::Value *RethrowFn =
Owen Anderson41a75022009-08-13 21:57:51 +00001636 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
David Chisnall9a2073c2010-01-06 18:02:59 +00001637 Params, false), "_Unwind_Resume");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001638
1639 bool isTry = isa<ObjCAtTryStmt>(S);
1640 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1641 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1642 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattner9fea9442009-05-11 18:16:28 +00001643 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001644 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1645 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1646 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1647
David Chisnall3a509cd2009-12-24 02:26:34 +00001648 // @synchronized()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001649 if (!isTry) {
Chris Lattner9fea9442009-05-11 18:16:28 +00001650 std::vector<const llvm::Type*> Args(1, IdTy);
1651 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001652 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001653 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
Mike Stump11289f42009-09-09 15:08:12 +00001654 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001655 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1656 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1657 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001658 }
1659
Chris Lattner9fea9442009-05-11 18:16:28 +00001660
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001661 // Push an EH context entry, used for handling rethrows and jumps
1662 // through finally.
1663 CGF.PushCleanupBlock(FinallyBlock);
1664
1665 // Emit the statements in the @try {} block
1666 CGF.setInvokeDest(TryHandler);
1667
1668 CGF.EmitBlock(TryBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001669 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001670 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1671
1672 // Jump to @finally if there is no exception
1673 CGF.EmitBranchThroughCleanup(FinallyEnd);
1674
1675 // Emit the handlers
1676 CGF.EmitBlock(TryHandler);
1677
Chris Lattner96afab52009-05-08 17:36:08 +00001678 // Get the correct versions of the exception handling intrinsics
1679 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Mike Stump11289f42009-09-09 15:08:12 +00001680 llvm::Value *llvm_eh_exception =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001681 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Duncan Sandscef56992009-10-14 16:13:30 +00001682 llvm::Value *llvm_eh_selector =
1683 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
1684 llvm::Value *llvm_eh_typeid_for =
1685 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001686
1687 // Exception object
1688 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1689 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1690
1691 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1692 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1693
1694 ESelArgs.push_back(Exc);
1695 ESelArgs.push_back(Personality);
1696
1697 bool HasCatchAll = false;
1698 // Only @try blocks are allowed @catch blocks, but both can have @finally
1699 if (isTry) {
1700 if (const ObjCAtCatchStmt* CatchStmt =
1701 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattner9fea9442009-05-11 18:16:28 +00001702 CGF.setInvokeDest(CatchInCatch);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001703
1704 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1705 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Mike Stumpdd93a192009-07-31 21:31:32 +00001706 Handlers.push_back(std::make_pair(CatchDecl,
1707 CatchStmt->getCatchBody()));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001708
1709 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff7cae42b2009-07-10 23:34:53 +00001710 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001711 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1712 // Use i8* null here to signal this is a catch all, not a cleanup.
1713 ESelArgs.push_back(NULLPtr);
1714 HasCatchAll = true;
1715 // No further catches after this one will ever by reached
1716 break;
Mike Stump11289f42009-09-09 15:08:12 +00001717 }
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001718
1719 // All other types should be Objective-C interface pointer types.
Mike Stump11289f42009-09-09 15:08:12 +00001720 const ObjCObjectPointerType *OPT =
John McCall9dd450b2009-09-21 23:43:11 +00001721 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001722 assert(OPT && "Invalid @catch type.");
Mike Stump11289f42009-09-09 15:08:12 +00001723 const ObjCInterfaceType *IT =
John McCall9dd450b2009-09-21 23:43:11 +00001724 OPT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001725 assert(IT && "Invalid @catch type.");
Chris Lattner96afab52009-05-08 17:36:08 +00001726 llvm::Value *EHType =
1727 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001728 ESelArgs.push_back(EHType);
1729 }
1730 }
1731 }
1732
1733 // We use a cleanup unless there was already a catch all.
1734 if (!HasCatchAll) {
Owen Anderson41a75022009-08-13 21:57:51 +00001735 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001736 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1737 }
1738
1739 // Find which handler was matched.
Chris Lattner96afab52009-05-08 17:36:08 +00001740 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001741 ESelArgs.begin(), ESelArgs.end(), "selector");
1742
1743 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1744 const ParmVarDecl *CatchParam = Handlers[i].first;
1745 const Stmt *CatchBody = Handlers[i].second;
1746
1747 llvm::BasicBlock *Next = 0;
1748
1749 // The last handler always matches.
1750 if (i + 1 != e) {
1751 assert(CatchParam && "Only last handler can be a catch all.");
1752
1753 // Test whether this block matches the type for the selector and branch
1754 // to Match if it does, or to the next BB if it doesn't.
1755 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1756 Next = CGF.createBasicBlock("catch.next");
Chris Lattner96afab52009-05-08 17:36:08 +00001757 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001758 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1759 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1760 Next);
1761
1762 CGF.EmitBlock(Match);
1763 }
Mike Stump11289f42009-09-09 15:08:12 +00001764
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001765 if (CatchBody) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001766 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1767 CGF.ConvertType(CatchParam->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001768
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001769 // Bind the catch parameter if it exists.
1770 if (CatchParam) {
1771 // CatchParam is a ParmVarDecl because of the grammar
1772 // construction used to handle this, but for codegen purposes
1773 // we treat this as a local decl.
1774 CGF.EmitLocalBlockVarDecl(*CatchParam);
1775 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1776 }
1777
1778 CGF.ObjCEHValueStack.push_back(ExcObject);
1779 CGF.EmitStmt(CatchBody);
1780 CGF.ObjCEHValueStack.pop_back();
1781
1782 CGF.EmitBranchThroughCleanup(FinallyEnd);
1783
1784 if (Next)
1785 CGF.EmitBlock(Next);
1786 } else {
1787 assert(!Next && "catchup should be last handler.");
1788
1789 CGF.Builder.CreateStore(Exc, RethrowPtr);
1790 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1791 }
1792 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001793 // The @finally block is a secondary landing pad for any exceptions thrown in
1794 // @catch() blocks
1795 CGF.EmitBlock(CatchInCatch);
1796 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1797 ESelArgs.clear();
1798 ESelArgs.push_back(Exc);
1799 ESelArgs.push_back(Personality);
David Chisnall3a509cd2009-12-24 02:26:34 +00001800 // If there is a @catch or @finally clause in outside of this one then we
1801 // need to make sure that we catch and rethrow it.
1802 if (PrevLandingPad) {
1803 ESelArgs.push_back(NULLPtr);
1804 } else {
1805 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
1806 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001807 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1808 "selector");
1809 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1810 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1811 CGF.Builder.CreateStore(Exc, RethrowPtr);
1812 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1813
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001814 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1815
1816 CGF.setInvokeDest(PrevLandingPad);
1817
1818 CGF.EmitBlock(FinallyBlock);
1819
Chris Lattner9fea9442009-05-11 18:16:28 +00001820
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001821 if (isTry) {
Mike Stump11289f42009-09-09 15:08:12 +00001822 if (const ObjCAtFinallyStmt* FinallyStmt =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001823 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1824 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1825 } else {
Chris Lattner9fea9442009-05-11 18:16:28 +00001826 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001827 // @synchronized.
Chris Lattner9fea9442009-05-11 18:16:28 +00001828 std::vector<const llvm::Type*> Args(1, IdTy);
1829 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001830 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001831 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Mike Stump11289f42009-09-09 15:08:12 +00001832 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001833 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1834 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1835 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001836 }
1837
1838 if (Info.SwitchBlock)
1839 CGF.EmitBlock(Info.SwitchBlock);
1840 if (Info.EndBlock)
1841 CGF.EmitBlock(Info.EndBlock);
1842
1843 // Branch around the rethrow code.
1844 CGF.EmitBranch(FinallyEnd);
1845
1846 CGF.EmitBlock(FinallyRethrow);
David Chisnall3a509cd2009-12-24 02:26:34 +00001847
1848 llvm::Value *ExceptionObject = CGF.Builder.CreateLoad(RethrowPtr);
1849 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1850 if (!UnwindBB) {
1851 CGF.Builder.CreateCall(RethrowFn, ExceptionObject);
1852 // Exception always thrown, next instruction is never reached.
1853 CGF.Builder.CreateUnreachable();
1854 } else {
1855 // If there is a @catch block outside this scope, we invoke instead of
1856 // calling because we may return to this function. This is very slow, but
1857 // some people still do it. It would be nice to add an optimised path for
1858 // this.
1859 CGF.Builder.CreateInvoke(RethrowFn, UnwindBB, UnwindBB, &ExceptionObject,
1860 &ExceptionObject+1);
1861 }
Mike Stump11289f42009-09-09 15:08:12 +00001862
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001863 CGF.EmitBlock(FinallyEnd);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001864}
1865
1866void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001867 const ObjCAtThrowStmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001868 llvm::Value *ExceptionAsObject;
1869
1870 std::vector<const llvm::Type*> Args(1, IdTy);
1871 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001872 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Mike Stump11289f42009-09-09 15:08:12 +00001873 llvm::Value *ThrowFn =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001874 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Mike Stump11289f42009-09-09 15:08:12 +00001875
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001876 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1877 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian078cd522009-05-17 16:49:27 +00001878 ExceptionAsObject = Exception;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001879 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001880 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001881 "Unexpected rethrow outside @catch block.");
1882 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1883 }
Fariborz Jahanian078cd522009-05-17 16:49:27 +00001884 ExceptionAsObject =
1885 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +00001886
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001887 // Note: This may have to be an invoke, if we want to support constructs like:
1888 // @try {
1889 // @throw(obj);
1890 // }
1891 // @catch(id) ...
1892 //
1893 // This is effectively turning @throw into an incredibly-expensive goto, but
1894 // it may happen as a result of inlining followed by missed optimizations, or
1895 // as a result of stupidity.
1896 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1897 if (!UnwindBB) {
1898 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1899 CGF.Builder.CreateUnreachable();
1900 } else {
1901 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1902 &ExceptionAsObject+1);
1903 }
1904 // Clear the insertion point to indicate we are in unreachable code.
1905 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001906}
1907
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001908llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001909 llvm::Value *AddrWeakObj) {
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00001910 return 0;
1911}
1912
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001913void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001914 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001915 return;
1916}
1917
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001918void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001919 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001920 return;
1921}
1922
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001923void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001924 llvm::Value *src, llvm::Value *dst,
1925 llvm::Value *ivarOffset) {
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001926 return;
1927}
1928
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001929void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001930 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001931 return;
1932}
1933
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001934void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001935 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001936 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +00001937 QualType Ty) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001938 return;
1939}
1940
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001941llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1942 const ObjCInterfaceDecl *ID,
1943 const ObjCIvarDecl *Ivar) {
1944 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1945 + '.' + Ivar->getNameAsString();
1946 // Emit the variable and initialize it with what we think the correct value
1947 // is. This allows code compiled with non-fragile ivars to work correctly
1948 // when linked against code which isn't (most of the time).
David Chisnall5778fce2009-08-31 16:41:57 +00001949 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1950 if (!IvarOffsetPointer) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001951 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1952 llvm::ConstantInt *OffsetGuess =
David Chisnallc8fc5732010-01-11 19:02:35 +00001953 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
David Chisnall5778fce2009-08-31 16:41:57 +00001954 // Don't emit the guess in non-PIC code because the linker will not be able
1955 // to replace it with the real version for a library. In non-PIC code you
1956 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump11289f42009-09-09 15:08:12 +00001957 // GCC-compiled class.
David Chisnall5778fce2009-08-31 16:41:57 +00001958 if (CGM.getLangOptions().PICLevel) {
1959 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
1960 llvm::Type::getInt32Ty(VMContext), false,
1961 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
1962 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
1963 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
1964 IvarOffsetGV, Name);
1965 } else {
1966 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001967 llvm::Type::getInt32PtrTy(VMContext), false,
1968 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall5778fce2009-08-31 16:41:57 +00001969 }
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001970 }
David Chisnall5778fce2009-08-31 16:41:57 +00001971 return IvarOffsetPointer;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001972}
1973
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001974LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1975 QualType ObjectTy,
1976 llvm::Value *BaseValue,
1977 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001978 unsigned CVRQualifiers) {
John McCall9dd450b2009-09-21 23:43:11 +00001979 const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00001980 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1981 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00001982}
Mike Stumpdd93a192009-07-31 21:31:32 +00001983
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001984static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1985 const ObjCInterfaceDecl *OID,
1986 const ObjCIvarDecl *OIVD) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001987 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001988 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001989 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1990 if (OIVD == Ivars[k])
1991 return OID;
1992 }
Mike Stump11289f42009-09-09 15:08:12 +00001993
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001994 // Otherwise check in the super class.
1995 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1996 return FindIvarInterface(Context, Super, OIVD);
Mike Stump11289f42009-09-09 15:08:12 +00001997
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001998 return 0;
1999}
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002000
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002001llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00002002 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002003 const ObjCIvarDecl *Ivar) {
David Chisnall5778fce2009-08-31 16:41:57 +00002004 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002005 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall5778fce2009-08-31 16:41:57 +00002006 return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2007 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar"));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002008 }
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002009 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002010 return llvm::ConstantInt::get(LongTy, Offset, "ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002011}
2012
Mike Stumpdd93a192009-07-31 21:31:32 +00002013CodeGen::CGObjCRuntime *
2014CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) {
Chris Lattner87ab27d2008-06-26 04:19:03 +00002015 return new CGObjCGNU(CGM);
Chris Lattnerb7256cd2008-03-01 08:50:34 +00002016}