blob: d69477852a7daff9b307a4fdcc7656225a68e4a2 [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);
David Chisnall92b762e2010-02-03 02:09:30 +0000149 virtual llvm::Constant *GetConstantSelector(Selector Sel);
150 virtual llvm::Constant *GetConstantTypedSelector(
151 const ObjCMethodDecl *Method);
152 llvm::Value *GetSelector(CGBuilderTy &Builder,
153 Selector Sel) {
154 return cast<llvm::Constant>((GetConstantSelector(Sel)));
155 }
156 llvm::Value *GetSelector(CGBuilderTy &Builder,
157 const ObjCMethodDecl *Method) {
158 return cast<llvm::Constant>(GetConstantTypedSelector(Method));
159 }
Mike Stump11289f42009-09-09 15:08:12 +0000160
161 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000162 const ObjCContainerDecl *CD);
Daniel Dunbar92992502008-08-15 22:20:32 +0000163 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
164 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000165 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000166 const ObjCProtocolDecl *PD);
167 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000168 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbara91c3e02008-09-24 03:38:44 +0000169 virtual llvm::Function *GetPropertyGetFunction();
170 virtual llvm::Function *GetPropertySetFunction();
Daniel Dunbarc46a0792009-07-24 07:40:24 +0000171 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000172
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000173 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
174 const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000175 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
176 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000177 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000178 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000179 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
180 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000181 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
182 llvm::Value *src, llvm::Value *dest);
Fariborz Jahaniane881b532008-11-20 19:23:36 +0000183 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000184 llvm::Value *src, llvm::Value *dest,
185 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000186 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
187 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000188 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +0000189 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000190 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000191 QualType Ty);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000192 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
193 QualType ObjectTy,
194 llvm::Value *BaseValue,
195 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000196 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000197 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +0000198 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000199 const ObjCIvarDecl *Ivar);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000200};
201} // end anonymous namespace
202
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000203
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000204/// Emits a reference to a dummy variable which is emitted with each class.
205/// This ensures that a linker error will be generated when trying to link
206/// together modules where a referenced class is not defined.
Mike Stumpdd93a192009-07-31 21:31:32 +0000207void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000208 std::string symbolRef = "__objc_class_ref_" + className;
209 // Don't emit two copies of the same symbol
Mike Stumpdd93a192009-07-31 21:31:32 +0000210 if (TheModule.getGlobalVariable(symbolRef))
211 return;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000212 std::string symbolName = "__objc_class_name_" + className;
213 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
214 if (!ClassSymbol) {
Owen Andersonc10c8d32009-07-08 19:05:04 +0000215 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
216 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000217 }
Owen Andersonc10c8d32009-07-08 19:05:04 +0000218 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerc58e5692009-08-05 05:25:18 +0000219 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000220}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000221
222static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000223 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000224}
225
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000226static std::string SymbolNameForMethod(const std::string &ClassName, const
227 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
228{
David Chisnall035ead22010-01-14 14:08:19 +0000229 std::string MethodNameColonStripped = MethodName;
230 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
231 ':', '_');
232 return std::string(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
233 CategoryName + "_" + MethodNameColonStripped;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000234}
235
Chris Lattner87ab27d2008-06-26 04:19:03 +0000236CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar566421c2009-05-04 15:31:17 +0000237 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Anderson170229f2009-07-14 23:10:40 +0000238 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000239 IntTy = cast<llvm::IntegerType>(
240 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
241 LongTy = cast<llvm::IntegerType>(
242 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000243
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000244 Int8Ty = llvm::Type::getInt8Ty(VMContext);
245 // C string type. Used in lots of places.
246 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
247
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000248 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000249 Zeros[1] = Zeros[0];
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000250 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner4bd55962008-03-30 23:03:07 +0000251 // Get the selector Type.
David Chisnall481e3a82010-01-23 02:40:42 +0000252 QualType selTy = CGM.getContext().getObjCSelType();
253 if (QualType() == selTy) {
254 SelectorTy = PtrToInt8Ty;
255 } else {
256 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
257 }
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000258
Owen Anderson9793f0e2009-07-29 22:16:19 +0000259 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner4bd55962008-03-30 23:03:07 +0000260 PtrTy = PtrToInt8Ty;
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattner4bd55962008-03-30 23:03:07 +0000262 // Object type
David Chisnall9f57c292009-08-17 16:35:33 +0000263 ASTIdTy = CGM.getContext().getObjCIdType();
David Chisnall481e3a82010-01-23 02:40:42 +0000264 if (QualType() == ASTIdTy) {
265 IdTy = PtrToInt8Ty;
266 } else {
267 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
268 }
Mike Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattner4bd55962008-03-30 23:03:07 +0000270 // IMP type
271 std::vector<const llvm::Type*> IMPArgs;
272 IMPArgs.push_back(IdTy);
273 IMPArgs.push_back(SelectorTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000274 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000275}
Mike Stumpdd93a192009-07-31 21:31:32 +0000276
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000277// This has to perform the lookup every time, since posing and related
278// techniques can modify the name -> class mapping.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000279llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000280 const ObjCInterfaceDecl *OID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000281 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
David Chisnalldf349172010-01-08 00:14:31 +0000282 // With the incompatible ABI, this will need to be replaced with a direct
283 // reference to the class symbol. For the compatible nonfragile ABI we are
284 // still performing this lookup at run time but emitting the symbol for the
285 // class externally so that we can make the switch later.
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000286 EmitClassRef(OID->getNameAsString());
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000287 ClassName = Builder.CreateStructGEP(ClassName, 0);
288
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000289 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000290 llvm::Constant *ClassLookupFn =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000291 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000292 Params,
293 true),
294 "objc_lookup_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000295 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner4bd55962008-03-30 23:03:07 +0000296}
297
David Chisnall92b762e2010-02-03 02:09:30 +0000298llvm::Constant *CGObjCGNU::GetConstantSelector(Selector Sel) {
Chris Lattnere4b95692008-11-24 03:33:13 +0000299 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner6d522c02008-06-26 04:37:12 +0000300 if (US == 0)
David Chisnall92b762e2010-02-03 02:09:30 +0000301 US = new llvm::GlobalAlias(SelectorTy,
David Chisnall9f57c292009-08-17 16:35:33 +0000302 llvm::GlobalValue::PrivateLinkage,
303 ".objc_untyped_selector_alias"+Sel.getAsString(),
Chris Lattner6d522c02008-06-26 04:37:12 +0000304 NULL, &TheModule);
Mike Stump11289f42009-09-09 15:08:12 +0000305
David Chisnall92b762e2010-02-03 02:09:30 +0000306 return US;
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000307}
308
David Chisnall92b762e2010-02-03 02:09:30 +0000309llvm::Constant *CGObjCGNU::GetConstantTypedSelector(const ObjCMethodDecl
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000310 *Method) {
311
312 std::string SelName = Method->getSelector().getAsString();
313 std::string SelTypes;
314 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
315 // Typed selectors
316 TypedSelector Selector = TypedSelector(SelName,
317 SelTypes);
318
319 // If it's already cached, return it.
Mike Stumpdd93a192009-07-31 21:31:32 +0000320 if (TypedSelectors[Selector]) {
David Chisnall92b762e2010-02-03 02:09:30 +0000321 return TypedSelectors[Selector];
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000322 }
323
324 // If it isn't, cache it.
325 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
David Chisnall92b762e2010-02-03 02:09:30 +0000326 SelectorTy,
David Chisnall9f57c292009-08-17 16:35:33 +0000327 llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000328 NULL, &TheModule);
329 TypedSelectors[Selector] = Sel;
330
David Chisnall92b762e2010-02-03 02:09:30 +0000331 return Sel;
Chris Lattner6d522c02008-06-26 04:37:12 +0000332}
333
Chris Lattnerd9b98862008-06-26 04:44:19 +0000334llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
335 const std::string &Name) {
David Chisnall5778fce2009-08-31 16:41:57 +0000336 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Owen Andersonade90fd2009-07-29 18:54:39 +0000337 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000338}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000339llvm::Constant *CGObjCGNU::ExportUniqueString(const std::string &Str,
340 const std::string prefix) {
341 std::string name = prefix + Str;
342 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
343 if (!ConstStr) {
344 llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
345 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
346 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
347 }
348 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
349}
Mike Stumpdd93a192009-07-31 21:31:32 +0000350
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000351llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000352 std::vector<llvm::Constant*> &V, const std::string &Name,
353 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson0e0189d2009-07-27 22:29:56 +0000354 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000355 return new llvm::GlobalVariable(TheModule, Ty, false,
356 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000357}
Mike Stumpdd93a192009-07-31 21:31:32 +0000358
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000359llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
David Chisnalldf349172010-01-08 00:14:31 +0000360 std::vector<llvm::Constant*> &V, const std::string &Name,
361 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson47034e12009-07-28 18:33:04 +0000362 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000363 return new llvm::GlobalVariable(TheModule, Ty, false,
Mike Stumpdd93a192009-07-31 21:31:32 +0000364 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000365}
366
367/// Generate an NSConstantString object.
David Chisnall481e3a82010-01-23 02:40:42 +0000368llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall358e7512010-01-27 12:49:23 +0000369
David Chisnall481e3a82010-01-23 02:40:42 +0000370 std::string Str(SL->getStrData(), SL->getByteLength());
371
David Chisnall358e7512010-01-27 12:49:23 +0000372 // Look for an existing one
373 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
374 if (old != ObjCStrings.end())
375 return old->getValue();
376
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000377 std::vector<llvm::Constant*> Ivars;
378 Ivars.push_back(NULLPtr);
Chris Lattner091f6982008-06-21 21:44:18 +0000379 Ivars.push_back(MakeConstantString(Str));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000380 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000381 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson758428f2009-08-05 23:18:46 +0000382 llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000383 Ivars, ".objc_str");
David Chisnall358e7512010-01-27 12:49:23 +0000384 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
385 ObjCStrings[Str] = ObjCStr;
386 ConstantStrings.push_back(ObjCStr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000387 return ObjCStr;
388}
389
390///Generates a message send where the super is the receiver. This is a message
391///send to self with special delivery semantics indicating which class's method
392///should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000393CodeGen::RValue
394CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000395 QualType ResultType,
396 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000397 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000398 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000399 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000400 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000401 const CallArgList &CallArgs,
402 const ObjCMethodDecl *Method) {
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000403 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
404
405 CallArgList ActualArgs;
406
407 ActualArgs.push_back(
David Chisnall9f57c292009-08-17 16:35:33 +0000408 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
409 ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000410 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
411 CGF.getContext().getObjCSelType()));
412 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
413
414 CodeGenTypes &Types = CGM.getTypes();
415 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000416 const llvm::FunctionType *impType =
417 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000418
Daniel Dunbar566421c2009-05-04 15:31:17 +0000419 llvm::Value *ReceiverClass = 0;
Chris Lattnera02cb802009-05-08 15:39:58 +0000420 if (isCategoryImpl) {
421 llvm::Constant *classLookupFunction = 0;
422 std::vector<const llvm::Type*> Params;
423 Params.push_back(PtrTy);
424 if (IsClassMessage) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000425 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000426 IdTy, Params, true), "objc_get_meta_class");
427 } else {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000428 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000429 IdTy, Params, true), "objc_get_class");
Daniel Dunbar566421c2009-05-04 15:31:17 +0000430 }
Chris Lattnera02cb802009-05-08 15:39:58 +0000431 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
432 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000433 } else {
Chris Lattnera02cb802009-05-08 15:39:58 +0000434 // Set up global aliases for the metaclass or class pointer if they do not
435 // already exist. These will are forward-references which will be set to
Mike Stumpdd93a192009-07-31 21:31:32 +0000436 // pointers to the class and metaclass structure created for the runtime
437 // load function. To send a message to super, we look up the value of the
Chris Lattnera02cb802009-05-08 15:39:58 +0000438 // super_class pointer from either the class or metaclass structure.
439 if (IsClassMessage) {
440 if (!MetaClassPtrAlias) {
441 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
442 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
443 Class->getNameAsString(), NULL, &TheModule);
444 }
445 ReceiverClass = MetaClassPtrAlias;
446 } else {
447 if (!ClassPtrAlias) {
448 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
449 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
450 Class->getNameAsString(), NULL, &TheModule);
451 }
452 ReceiverClass = ClassPtrAlias;
Daniel Dunbar566421c2009-05-04 15:31:17 +0000453 }
Chris Lattnerc06ce0f2009-04-25 23:19:45 +0000454 }
Daniel Dunbar566421c2009-05-04 15:31:17 +0000455 // Cast the pointer to a simplified version of the class structure
Mike Stump11289f42009-09-09 15:08:12 +0000456 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000457 llvm::PointerType::getUnqual(
Owen Anderson758428f2009-08-05 23:18:46 +0000458 llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000459 // Get the superclass pointer
460 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
461 // Load the superclass pointer
462 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000463 // Construct the structure used to look up the IMP
Owen Anderson758428f2009-08-05 23:18:46 +0000464 llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
465 Receiver->getType(), IdTy, NULL);
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000466 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000467
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000468 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000469 CGF.Builder.CreateStore(ReceiverClass,
470 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000471
472 // Get the IMP
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000473 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000474 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000475 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000476 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000477 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
478 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000479 "objc_msg_lookup_super");
480
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000481 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000482 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000483 lookupArgs+2);
484
Anders Carlsson61a401c2009-12-24 19:25:24 +0000485 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000486}
487
Mike Stump11289f42009-09-09 15:08:12 +0000488/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000489CodeGen::RValue
490CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000491 QualType ResultType,
492 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000493 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000494 bool IsClassMessage,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000495 const CallArgList &CallArgs,
496 const ObjCMethodDecl *Method) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000497 CGBuilderTy &Builder = CGF.Builder;
David Chisnall9f57c292009-08-17 16:35:33 +0000498 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000499 llvm::Value *cmd;
500 if (Method)
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000501 cmd = GetSelector(Builder, Method);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000502 else
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000503 cmd = GetSelector(Builder, Sel);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000504 CallArgList ActualArgs;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000505
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000506 Receiver = Builder.CreateBitCast(Receiver, IdTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000507 ActualArgs.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000508 std::make_pair(RValue::get(Receiver), ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000509 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
510 CGF.getContext().getObjCSelType()));
511 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
512
513 CodeGenTypes &Types = CGM.getTypes();
514 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000515 const llvm::FunctionType *impType =
516 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000517
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000518 llvm::Value *imp;
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000519 // For sender-aware dispatch, we pass the sender as the third argument to a
520 // lookup function. When sending messages from C code, the sender is nil.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000521 // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
522 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
523
524 std::vector<const llvm::Type*> Params;
525 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
526 Builder.CreateStore(Receiver, ReceiverPtr);
527 Params.push_back(ReceiverPtr->getType());
528 Params.push_back(SelectorTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000529 llvm::Value *self;
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000530
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000531 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
532 self = CGF.LoadObjCSelf();
533 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +0000534 self = llvm::ConstantPointerNull::get(IdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000535 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000536
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000537 Params.push_back(self->getType());
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000538
539 // The lookup function returns a slot, which can be safely cached.
540 llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy,
541 IntTy, llvm::PointerType::getUnqual(impType), NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000542 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000543 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000544 llvm::PointerType::getUnqual(SlotTy), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000545 "objc_msg_lookup_sender");
546
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000547 // The lookup function is guaranteed not to capture the receiver pointer.
548 if (llvm::Function *LookupFn = dyn_cast<llvm::Function>(lookupFunction)) {
549 LookupFn->setDoesNotCapture(1);
550 }
551
552 llvm::Value *slot =
553 Builder.CreateCall3(lookupFunction, ReceiverPtr, cmd, self);
554 imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
555 // The lookup function may have changed the receiver, so make sure we use
556 // the new one.
557 ActualArgs[0] =
558 std::make_pair(RValue::get(Builder.CreateLoad(ReceiverPtr)), ASTIdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000559 } else {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000560 std::vector<const llvm::Type*> Params;
561 Params.push_back(Receiver->getType());
562 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000563 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000564 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
565 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000566 "objc_msg_lookup");
567
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000568 imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000569 }
Chris Lattnerdbcc2ca2008-05-06 00:56:42 +0000570
Anders Carlsson61a401c2009-12-24 19:25:24 +0000571 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000572}
573
Mike Stump11289f42009-09-09 15:08:12 +0000574/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000575/// objc_category structures.
576llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Mike Stump11289f42009-09-09 15:08:12 +0000577 const std::string &CategoryName,
578 const llvm::SmallVectorImpl<Selector> &MethodSels,
579 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000580 bool isClassMethodList) {
David Chisnall9f57c292009-08-17 16:35:33 +0000581 if (MethodSels.empty())
582 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000583 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000584 llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000585 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
586 PtrToInt8Ty, // Method types
Owen Anderson9793f0e2009-07-29 22:16:19 +0000587 llvm::PointerType::getUnqual(IMPTy), //Method pointer
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000588 NULL);
589 std::vector<llvm::Constant*> Methods;
590 std::vector<llvm::Constant*> Elements;
591 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
592 Elements.clear();
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000593 if (llvm::Constant *Method =
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000594 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnere4b95692008-11-24 03:33:13 +0000595 MethodSels[i].getAsString(),
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000596 isClassMethodList))) {
David Chisnall5778fce2009-08-31 16:41:57 +0000597 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
598 Elements.push_back(C);
599 Elements.push_back(MethodTypes[i]);
Owen Andersonade90fd2009-07-29 18:54:39 +0000600 Method = llvm::ConstantExpr::getBitCast(Method,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000601 llvm::PointerType::getUnqual(IMPTy));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000602 Elements.push_back(Method);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000603 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000604 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000605 }
606
607 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000608 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000609 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000610 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattner882034d2008-06-26 04:52:29 +0000611 Methods);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000612
613 // Structure containing list pointer, array and array count
614 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Andersonc36edfe2009-08-13 23:27:53 +0000615 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000616 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Owen Anderson758428f2009-08-05 23:18:46 +0000617 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
Mike Stump11289f42009-09-09 15:08:12 +0000618 NextPtrTy,
619 IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000620 ObjCMethodArrayTy,
621 NULL);
622 // Refine next pointer type to concrete type
623 llvm::cast<llvm::OpaqueType>(
624 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
625 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
626
627 Methods.clear();
Owen Anderson7ec07a52009-07-30 23:11:26 +0000628 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +0000629 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson41a75022009-08-13 21:57:51 +0000630 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000631 MethodTypes.size()));
632 Methods.push_back(MethodArray);
Mike Stump11289f42009-09-09 15:08:12 +0000633
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000634 // Create an instance of the structure
635 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
636}
637
638/// Generates an IvarList. Used in construction of a objc_class.
639llvm::Constant *CGObjCGNU::GenerateIvarList(
640 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
641 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
642 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
David Chisnallb3b44ce2009-11-16 19:05:54 +0000643 if (IvarNames.size() == 0)
644 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000645 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000646 llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000647 PtrToInt8Ty,
648 PtrToInt8Ty,
649 IntTy,
650 NULL);
651 std::vector<llvm::Constant*> Ivars;
652 std::vector<llvm::Constant*> Elements;
653 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
654 Elements.clear();
David Chisnall5778fce2009-08-31 16:41:57 +0000655 Elements.push_back(IvarNames[i]);
656 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000657 Elements.push_back(IvarOffsets[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000658 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000659 }
660
661 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000662 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000663 IvarNames.size());
664
Mike Stump11289f42009-09-09 15:08:12 +0000665
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000666 Elements.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000667 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson47034e12009-07-28 18:33:04 +0000668 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000669 // Structure containing array and array count
Owen Anderson758428f2009-08-05 23:18:46 +0000670 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000671 ObjCIvarArrayTy,
672 NULL);
673
674 // Create an instance of the structure
675 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
676}
677
678/// Generate a class structure
679llvm::Constant *CGObjCGNU::GenerateClassStructure(
680 llvm::Constant *MetaClass,
681 llvm::Constant *SuperClass,
682 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000683 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000684 llvm::Constant *Version,
685 llvm::Constant *InstanceSize,
686 llvm::Constant *IVars,
687 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000688 llvm::Constant *Protocols,
689 llvm::Constant *IvarOffsets,
690 llvm::Constant *Properties) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000691 // Set up the class structure
692 // Note: Several of these are char*s when they should be ids. This is
693 // because the runtime performs this translation on load.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000694 //
695 // Fields marked New ABI are part of the GNUstep runtime. We emit them
696 // anyway; the classes will still work with the GNU runtime, they will just
697 // be ignored.
Owen Anderson758428f2009-08-05 23:18:46 +0000698 llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000699 PtrToInt8Ty, // class_pointer
700 PtrToInt8Ty, // super_class
701 PtrToInt8Ty, // name
702 LongTy, // version
703 LongTy, // info
704 LongTy, // instance_size
705 IVars->getType(), // ivars
706 Methods->getType(), // methods
Mike Stump11289f42009-09-09 15:08:12 +0000707 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000708 PtrTy, // dtable
709 PtrTy, // subclass_list
710 PtrTy, // sibling_class
711 PtrTy, // protocols
712 PtrTy, // gc_object_type
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000713 // New ABI:
714 LongTy, // abi_version
715 IvarOffsets->getType(), // ivar_offsets
716 Properties->getType(), // properties
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000717 NULL);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000718 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000719 // Fill in the structure
720 std::vector<llvm::Constant*> Elements;
Owen Andersonade90fd2009-07-29 18:54:39 +0000721 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000722 Elements.push_back(SuperClass);
Chris Lattnerda35bc82008-06-26 04:47:04 +0000723 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000724 Elements.push_back(Zero);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000725 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000726 Elements.push_back(InstanceSize);
727 Elements.push_back(IVars);
728 Elements.push_back(Methods);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000729 Elements.push_back(NULLPtr);
730 Elements.push_back(NULLPtr);
731 Elements.push_back(NULLPtr);
Owen Andersonade90fd2009-07-29 18:54:39 +0000732 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000733 Elements.push_back(NULLPtr);
734 Elements.push_back(Zero);
735 Elements.push_back(IvarOffsets);
736 Elements.push_back(Properties);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000737 // Create an instance of the structure
David Chisnalldf349172010-01-08 00:14:31 +0000738 // This is now an externally visible symbol, so that we can speed up class
739 // messages in the next ABI.
740 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name),
741 llvm::GlobalValue::ExternalLinkage);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000742}
743
744llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
745 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
746 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
Mike Stump11289f42009-09-09 15:08:12 +0000747 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000748 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000749 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
750 PtrToInt8Ty,
751 NULL);
752 std::vector<llvm::Constant*> Methods;
753 std::vector<llvm::Constant*> Elements;
754 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
755 Elements.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000756 Elements.push_back(MethodNames[i]);
David Chisnall5778fce2009-08-31 16:41:57 +0000757 Elements.push_back(MethodTypes[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000758 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000759 }
Owen Anderson9793f0e2009-07-29 22:16:19 +0000760 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000761 MethodNames.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000762 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpdd93a192009-07-31 21:31:32 +0000763 Methods);
Owen Anderson758428f2009-08-05 23:18:46 +0000764 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000765 IntTy, ObjCMethodArrayTy, NULL);
766 Methods.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000767 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000768 Methods.push_back(Array);
769 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
770}
Mike Stumpdd93a192009-07-31 21:31:32 +0000771
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000772// Create the protocol list structure used in classes, categories and so on
773llvm::Constant *CGObjCGNU::GenerateProtocolList(
774 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000775 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000776 Protocols.size());
Owen Anderson758428f2009-08-05 23:18:46 +0000777 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000778 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
779 LongTy,//FIXME: Should be size_t
780 ProtocolArrayTy,
781 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000782 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000783 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
784 iter != endIter ; iter++) {
David Chisnallbc8bdea2009-11-20 14:50:59 +0000785 llvm::Constant *protocol = 0;
786 llvm::StringMap<llvm::Constant*>::iterator value =
787 ExistingProtocols.find(*iter);
788 if (value == ExistingProtocols.end()) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000789 protocol = GenerateEmptyProtocol(*iter);
David Chisnallbc8bdea2009-11-20 14:50:59 +0000790 } else {
791 protocol = value->getValue();
792 }
Owen Andersonade90fd2009-07-29 18:54:39 +0000793 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Anderson170229f2009-07-14 23:10:40 +0000794 PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000795 Elements.push_back(Ptr);
796 }
Owen Anderson47034e12009-07-28 18:33:04 +0000797 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000798 Elements);
799 Elements.clear();
800 Elements.push_back(NULLPtr);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000801 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000802 Elements.push_back(ProtocolArray);
803 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
804}
805
Mike Stump11289f42009-09-09 15:08:12 +0000806llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000807 const ObjCProtocolDecl *PD) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000808 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Mike Stump11289f42009-09-09 15:08:12 +0000809 const llvm::Type *T =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000810 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000811 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000812}
813
814llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
815 const std::string &ProtocolName) {
816 llvm::SmallVector<std::string, 0> EmptyStringVector;
817 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
818
819 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000820 llvm::Constant *MethodList =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000821 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
822 // Protocols are objects containing lists of the methods implemented and
823 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +0000824 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000825 PtrToInt8Ty,
826 ProtocolList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000827 MethodList->getType(),
828 MethodList->getType(),
829 MethodList->getType(),
830 MethodList->getType(),
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000831 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000832 std::vector<llvm::Constant*> Elements;
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000833 // The isa pointer must be set to a magic number so the runtime knows it's
834 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000835 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
836 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +0000837 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000838 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000839 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
840 Elements.push_back(ProtocolList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000841 Elements.push_back(MethodList);
842 Elements.push_back(MethodList);
843 Elements.push_back(MethodList);
844 Elements.push_back(MethodList);
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000845 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000846}
847
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000848void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
849 ASTContext &Context = CGM.getContext();
Chris Lattner86d7d912008-11-24 03:54:41 +0000850 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000851 llvm::SmallVector<std::string, 16> Protocols;
852 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
853 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000854 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000855 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
856 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000857 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
858 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000859 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
860 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000861 std::string TypeStr;
862 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000863 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
864 InstanceMethodNames.push_back(
865 MakeConstantString((*iter)->getSelector().getAsString()));
866 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
867 } else {
868 OptionalInstanceMethodNames.push_back(
869 MakeConstantString((*iter)->getSelector().getAsString()));
870 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
871 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000872 }
873 // Collect information about class methods:
874 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
875 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000876 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
877 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +0000878 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000879 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
880 iter != endIter ; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000881 std::string TypeStr;
882 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000883 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
884 ClassMethodNames.push_back(
885 MakeConstantString((*iter)->getSelector().getAsString()));
886 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
887 } else {
888 OptionalClassMethodNames.push_back(
889 MakeConstantString((*iter)->getSelector().getAsString()));
890 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
891 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000892 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000893
894 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
895 llvm::Constant *InstanceMethodList =
896 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
897 llvm::Constant *ClassMethodList =
898 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000899 llvm::Constant *OptionalInstanceMethodList =
900 GenerateProtocolMethodList(OptionalInstanceMethodNames,
901 OptionalInstanceMethodTypes);
902 llvm::Constant *OptionalClassMethodList =
903 GenerateProtocolMethodList(OptionalClassMethodNames,
904 OptionalClassMethodTypes);
905
906 // Property metadata: name, attributes, isSynthesized, setter name, setter
907 // types, getter name, getter types.
908 // The isSynthesized value is always set to 0 in a protocol. It exists to
909 // simplify the runtime library by allowing it to use the same data
910 // structures for protocol metadata everywhere.
911 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
912 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
913 PtrToInt8Ty, NULL);
914 std::vector<llvm::Constant*> Properties;
915 std::vector<llvm::Constant*> OptionalProperties;
916
917 // Add all of the property methods need adding to the method list and to the
918 // property metadata list.
919 for (ObjCContainerDecl::prop_iterator
920 iter = PD->prop_begin(), endIter = PD->prop_end();
921 iter != endIter ; iter++) {
922 std::vector<llvm::Constant*> Fields;
923 ObjCPropertyDecl *property = (*iter);
924
925 Fields.push_back(MakeConstantString(property->getNameAsString()));
926 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
927 property->getPropertyAttributes()));
928 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
929 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
930 std::string TypeStr;
931 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
932 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
933 InstanceMethodTypes.push_back(TypeEncoding);
934 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
935 Fields.push_back(TypeEncoding);
936 } else {
937 Fields.push_back(NULLPtr);
938 Fields.push_back(NULLPtr);
939 }
940 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
941 std::string TypeStr;
942 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
943 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
944 InstanceMethodTypes.push_back(TypeEncoding);
945 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
946 Fields.push_back(TypeEncoding);
947 } else {
948 Fields.push_back(NULLPtr);
949 Fields.push_back(NULLPtr);
950 }
951 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
952 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
953 } else {
954 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
955 }
956 }
957 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
958 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
959 llvm::Constant* PropertyListInitFields[] =
960 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
961
962 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000963 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000964 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
965 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
966 PropertyListInit, ".objc_property_list");
967
968 llvm::Constant *OptionalPropertyArray =
969 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
970 OptionalProperties.size()) , OptionalProperties);
971 llvm::Constant* OptionalPropertyListInitFields[] = {
972 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
973 OptionalPropertyArray };
974
975 llvm::Constant *OptionalPropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000976 llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000977 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
978 OptionalPropertyListInit->getType(), false,
979 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
980 ".objc_property_list");
981
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000982 // Protocols are objects containing lists of the methods implemented and
983 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +0000984 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000985 PtrToInt8Ty,
986 ProtocolList->getType(),
987 InstanceMethodList->getType(),
988 ClassMethodList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000989 OptionalInstanceMethodList->getType(),
990 OptionalClassMethodList->getType(),
991 PropertyList->getType(),
992 OptionalPropertyList->getType(),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000993 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000994 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000995 // The isa pointer must be set to a magic number so the runtime knows it's
996 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000997 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
998 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +0000999 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001000 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001001 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1002 Elements.push_back(ProtocolList);
1003 Elements.push_back(InstanceMethodList);
1004 Elements.push_back(ClassMethodList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001005 Elements.push_back(OptionalInstanceMethodList);
1006 Elements.push_back(OptionalClassMethodList);
1007 Elements.push_back(PropertyList);
1008 Elements.push_back(OptionalPropertyList);
Mike Stump11289f42009-09-09 15:08:12 +00001009 ExistingProtocols[ProtocolName] =
Owen Andersonade90fd2009-07-29 18:54:39 +00001010 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001011 ".objc_protocol"), IdTy);
1012}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001013void CGObjCGNU::GenerateProtocolHolderCategory(void) {
1014 // Collect information about instance methods
1015 llvm::SmallVector<Selector, 1> MethodSels;
1016 llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
1017
1018 std::vector<llvm::Constant*> Elements;
1019 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1020 const std::string CategoryName = "AnotherHack";
1021 Elements.push_back(MakeConstantString(CategoryName));
1022 Elements.push_back(MakeConstantString(ClassName));
1023 // Instance method list
1024 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1025 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1026 // Class method list
1027 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1028 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1029 // Protocol list
1030 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1031 ExistingProtocols.size());
1032 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
1033 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1034 LongTy,//FIXME: Should be size_t
1035 ProtocolArrayTy,
1036 NULL);
1037 std::vector<llvm::Constant*> ProtocolElements;
1038 for (llvm::StringMapIterator<llvm::Constant*> iter =
1039 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1040 iter != endIter ; iter++) {
1041 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1042 PtrTy);
1043 ProtocolElements.push_back(Ptr);
1044 }
1045 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1046 ProtocolElements);
1047 ProtocolElements.clear();
1048 ProtocolElements.push_back(NULLPtr);
1049 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1050 ExistingProtocols.size()));
1051 ProtocolElements.push_back(ProtocolArray);
1052 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1053 ProtocolElements, ".objc_protocol_list"), PtrTy));
1054 Categories.push_back(llvm::ConstantExpr::getBitCast(
1055 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
1056 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1057}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001058
Daniel Dunbar92992502008-08-15 22:20:32 +00001059void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001060 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1061 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar92992502008-08-15 22:20:32 +00001062 // Collect information about instance methods
1063 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1064 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001065 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001066 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001067 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001068 InstanceMethodSels.push_back((*iter)->getSelector());
1069 std::string TypeStr;
1070 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001071 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001072 }
1073
1074 // Collect information about class methods
1075 llvm::SmallVector<Selector, 16> ClassMethodSels;
1076 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001077 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001078 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001079 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001080 ClassMethodSels.push_back((*iter)->getSelector());
1081 std::string TypeStr;
1082 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001083 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001084 }
1085
1086 // Collect the names of referenced protocols
1087 llvm::SmallVector<std::string, 16> Protocols;
1088 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
1089 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1090 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1091 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001092 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001093
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001094 std::vector<llvm::Constant*> Elements;
1095 Elements.push_back(MakeConstantString(CategoryName));
1096 Elements.push_back(MakeConstantString(ClassName));
Mike Stump11289f42009-09-09 15:08:12 +00001097 // Instance method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001098 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001099 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001100 false), PtrTy));
1101 // Class method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001102 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001103 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001104 PtrTy));
1105 // Protocol list
Owen Andersonade90fd2009-07-29 18:54:39 +00001106 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001107 GenerateProtocolList(Protocols), PtrTy));
Owen Andersonade90fd2009-07-29 18:54:39 +00001108 Categories.push_back(llvm::ConstantExpr::getBitCast(
Mike Stump11289f42009-09-09 15:08:12 +00001109 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson758428f2009-08-05 23:18:46 +00001110 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001111}
Daniel Dunbar92992502008-08-15 22:20:32 +00001112
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001113llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1114 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1115 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1116 ASTContext &Context = CGM.getContext();
1117 //
1118 // Property metadata: name, attributes, isSynthesized, setter name, setter
1119 // types, getter name, getter types.
1120 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1121 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1122 PtrToInt8Ty, NULL);
1123 std::vector<llvm::Constant*> Properties;
1124
1125
1126 // Add all of the property methods need adding to the method list and to the
1127 // property metadata list.
1128 for (ObjCImplDecl::propimpl_iterator
1129 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1130 iter != endIter ; iter++) {
1131 std::vector<llvm::Constant*> Fields;
1132 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
1133
1134 Fields.push_back(MakeConstantString(property->getNameAsString()));
1135 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1136 property->getPropertyAttributes()));
1137 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1138 (*iter)->getPropertyImplementation() ==
1139 ObjCPropertyImplDecl::Synthesize));
1140 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1141 InstanceMethodSels.push_back(getter->getSelector());
1142 std::string TypeStr;
1143 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1144 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1145 InstanceMethodTypes.push_back(TypeEncoding);
1146 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1147 Fields.push_back(TypeEncoding);
1148 } else {
1149 Fields.push_back(NULLPtr);
1150 Fields.push_back(NULLPtr);
1151 }
1152 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1153 InstanceMethodSels.push_back(setter->getSelector());
1154 std::string TypeStr;
1155 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1156 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1157 InstanceMethodTypes.push_back(TypeEncoding);
1158 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1159 Fields.push_back(TypeEncoding);
1160 } else {
1161 Fields.push_back(NULLPtr);
1162 Fields.push_back(NULLPtr);
1163 }
1164 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1165 }
1166 llvm::ArrayType *PropertyArrayTy =
1167 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1168 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1169 Properties);
1170 llvm::Constant* PropertyListInitFields[] =
1171 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1172
1173 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001174 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001175 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1176 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1177 ".objc_property_list");
1178}
1179
Daniel Dunbar92992502008-08-15 22:20:32 +00001180void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1181 ASTContext &Context = CGM.getContext();
1182
1183 // Get the superclass name.
Mike Stump11289f42009-09-09 15:08:12 +00001184 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar92992502008-08-15 22:20:32 +00001185 OID->getClassInterface()->getSuperClass();
Chris Lattner86d7d912008-11-24 03:54:41 +00001186 std::string SuperClassName;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001187 if (SuperClassDecl) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001188 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001189 EmitClassRef(SuperClassName);
1190 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001191
1192 // Get the class name
Chris Lattner87bc3872009-04-01 02:00:48 +00001193 ObjCInterfaceDecl *ClassDecl =
1194 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner86d7d912008-11-24 03:54:41 +00001195 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001196 // Emit the symbol that is used to generate linker errors if this class is
1197 // referenced in other modules but not declared.
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001198 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump11289f42009-09-09 15:08:12 +00001199 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001200 TheModule.getGlobalVariable(classSymbolName)) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001201 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001202 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00001203 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001204 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Andersonc10c8d32009-07-08 19:05:04 +00001205 classSymbolName);
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001206 }
Mike Stump11289f42009-09-09 15:08:12 +00001207
Daniel Dunbar12119b92009-05-03 10:46:44 +00001208 // Get the size of instances.
1209 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar92992502008-08-15 22:20:32 +00001210
1211 // Collect information about instance variables.
1212 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
1213 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
1214 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump11289f42009-09-09 15:08:12 +00001215
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001216 std::vector<llvm::Constant*> IvarOffsetValues;
1217
Mike Stump11289f42009-09-09 15:08:12 +00001218 int superInstanceSize = !SuperClassDecl ? 0 :
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001219 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
1220 // For non-fragile ivars, set the instance size to 0 - {the size of just this
1221 // class}. The runtime will then set this to the correct value on load.
1222 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1223 instanceSize = 0 - (instanceSize - superInstanceSize);
1224 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001225 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1226 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1227 // Store the name
David Chisnall5778fce2009-08-31 16:41:57 +00001228 IvarNames.push_back(MakeConstantString((*iter)->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001229 // Get the type encoding for this ivar
1230 std::string TypeStr;
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00001231 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001232 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001233 // Get the offset
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001234 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
David Chisnallcb1b7bf2009-11-17 19:32:15 +00001235 uint64_t Offset = BaseOffset;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001236 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001237 Offset = BaseOffset - superInstanceSize;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001238 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001239 IvarOffsets.push_back(
Owen Anderson41a75022009-08-13 21:57:51 +00001240 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001241 IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1242 false, llvm::GlobalValue::ExternalLinkage,
1243 llvm::ConstantInt::get(IntTy, BaseOffset),
1244 "__objc_ivar_offset_value_" + ClassName +"." +
1245 (*iter)->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001246 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001247 llvm::Constant *IvarOffsetArrayInit =
1248 llvm::ConstantArray::get(llvm::ArrayType::get(PtrToIntTy,
1249 IvarOffsetValues.size()), IvarOffsetValues);
1250 llvm::GlobalVariable *IvarOffsetArray = new llvm::GlobalVariable(TheModule,
1251 IvarOffsetArrayInit->getType(), false,
1252 llvm::GlobalValue::InternalLinkage, IvarOffsetArrayInit,
1253 ".ivar.offsets");
Daniel Dunbar92992502008-08-15 22:20:32 +00001254
1255 // Collect information about instance methods
1256 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1257 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001258 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001259 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001260 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001261 InstanceMethodSels.push_back((*iter)->getSelector());
1262 std::string TypeStr;
1263 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001264 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001265 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001266
1267 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1268 InstanceMethodTypes);
1269
Daniel Dunbar92992502008-08-15 22:20:32 +00001270
1271 // Collect information about class methods
1272 llvm::SmallVector<Selector, 16> ClassMethodSels;
1273 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001274 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001275 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001276 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001277 ClassMethodSels.push_back((*iter)->getSelector());
1278 std::string TypeStr;
1279 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001280 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001281 }
1282 // Collect the names of referenced protocols
1283 llvm::SmallVector<std::string, 16> Protocols;
1284 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1285 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1286 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001287 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001288
1289
1290
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001291 // Get the superclass pointer.
1292 llvm::Constant *SuperClass;
Chris Lattner86d7d912008-11-24 03:54:41 +00001293 if (!SuperClassName.empty()) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001294 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
1295 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +00001296 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001297 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001298 // Empty vector used to construct empty method lists
1299 llvm::SmallVector<llvm::Constant*, 1> empty;
1300 // Generate the method and instance variable lists
1301 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001302 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001303 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001304 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001305 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
1306 IvarOffsets);
Mike Stump11289f42009-09-09 15:08:12 +00001307 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall5778fce2009-08-31 16:41:57 +00001308 // we emit a symbol containing the offset for each ivar in the class. This
1309 // allows code compiled for the non-Fragile ABI to inherit from code compiled
1310 // for the legacy ABI, without causing problems. The converse is also
1311 // possible, but causes all ivar accesses to be fragile.
1312 int i = 0;
1313 // Offset pointer for getting at the correct field in the ivar list when
1314 // setting up the alias. These are: The base address for the global, the
1315 // ivar array (second field), the ivar in this list (set for each ivar), and
1316 // the offset (third field in ivar structure)
1317 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
1318 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump11289f42009-09-09 15:08:12 +00001319 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall5778fce2009-08-31 16:41:57 +00001320 llvm::ConstantInt::get(IndexTy, 2) };
1321
1322 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1323 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1324 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1325 +(*iter)->getNameAsString();
1326 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++);
1327 // Get the correct ivar field
1328 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1329 IvarList, offsetPointerIndexes, 4);
1330 // Get the existing alias, if one exists.
1331 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1332 if (offset) {
1333 offset->setInitializer(offsetValue);
1334 // If this is the real definition, change its linkage type so that
1335 // different modules will use this one, rather than their private
1336 // copy.
1337 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1338 } else {
1339 // Add a new alias if there isn't one already.
1340 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1341 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1342 }
1343 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001344 //Generate metaclass for class methods
1345 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnallb3b44ce2009-11-16 19:05:54 +00001346 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001347 empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001348
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001349 // Generate the class structure
Chris Lattner86d7d912008-11-24 03:54:41 +00001350 llvm::Constant *ClassStruct =
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001351 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner86d7d912008-11-24 03:54:41 +00001352 ClassName.c_str(), 0,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001353 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001354 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1355 Properties);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001356
1357 // Resolve the class aliases, if they exist.
1358 if (ClassPtrAlias) {
1359 ClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001360 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001361 ClassPtrAlias = 0;
1362 }
1363 if (MetaClassPtrAlias) {
1364 MetaClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001365 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001366 MetaClassPtrAlias = 0;
1367 }
1368
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001369 // Add class structure to list to be added to the symtab later
Owen Andersonade90fd2009-07-29 18:54:39 +00001370 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001371 Classes.push_back(ClassStruct);
1372}
1373
Fariborz Jahanian248c7192009-06-23 21:47:46 +00001374
Mike Stump11289f42009-09-09 15:08:12 +00001375llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001376 // Only emit an ObjC load function if no Objective-C stuff has been called
1377 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1378 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov3b6dd582008-06-01 15:14:46 +00001379 UntypedSelectors.empty())
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001380 return NULL;
Eli Friedman412c6682008-06-01 16:00:02 +00001381
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001382 // Add all referenced protocols to a category.
1383 GenerateProtocolHolderCategory();
1384
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001385 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1386 SelectorTy->getElementType());
1387 const llvm::Type *SelStructPtrTy = SelectorTy;
1388 bool isSelOpaque = false;
1389 if (SelStructTy == 0) {
Owen Anderson758428f2009-08-05 23:18:46 +00001390 SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
1391 PtrToInt8Ty, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001392 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001393 isSelOpaque = true;
1394 }
1395
Eli Friedman412c6682008-06-01 16:00:02 +00001396 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001397 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman412c6682008-06-01 16:00:02 +00001398 TheModule.addTypeName(".objc_id", IdTy);
1399 TheModule.addTypeName(".objc_imp", IMPTy);
1400
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001401 std::vector<llvm::Constant*> Elements;
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001402 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001403 // Generate statics list:
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001404 if (ConstantStrings.size()) {
Owen Anderson9793f0e2009-07-29 22:16:19 +00001405 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001406 ConstantStrings.size() + 1);
1407 ConstantStrings.push_back(NULLPtr);
David Chisnall5778fce2009-08-31 16:41:57 +00001408
Daniel Dunbar75fa84e2009-11-29 02:38:47 +00001409 llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
1410 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall5778fce2009-08-31 16:41:57 +00001411 Elements.push_back(MakeConstantString(StringClass,
1412 ".objc_static_class_name"));
Owen Anderson47034e12009-07-28 18:33:04 +00001413 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001414 ConstantStrings));
Mike Stump11289f42009-09-09 15:08:12 +00001415 llvm::StructType *StaticsListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00001416 llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Anderson170229f2009-07-14 23:10:40 +00001417 llvm::Type *StaticsListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001418 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001419 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump11289f42009-09-09 15:08:12 +00001420 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001421 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001422 Elements.clear();
1423 Elements.push_back(Statics);
Owen Anderson0b75f232009-07-31 20:28:54 +00001424 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001425 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Andersonade90fd2009-07-29 18:54:39 +00001426 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001427 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001428 // Array of classes, categories, and constant objects
Owen Anderson9793f0e2009-07-29 22:16:19 +00001429 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001430 Classes.size() + Categories.size() + 2);
Mike Stump11289f42009-09-09 15:08:12 +00001431 llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00001432 LongTy, SelStructPtrTy,
Owen Anderson41a75022009-08-13 21:57:51 +00001433 llvm::Type::getInt16Ty(VMContext),
1434 llvm::Type::getInt16Ty(VMContext),
Chris Lattner63dd3372008-06-26 04:10:42 +00001435 ClassListTy, NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001436
1437 Elements.clear();
1438 // Pointer to an array of selectors used in this module.
1439 std::vector<llvm::Constant*> Selectors;
1440 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1441 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1442 iter != iterEnd ; ++iter) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001443 Elements.push_back(ExportUniqueString(iter->first.first, ".objc_sel_name"));
David Chisnall29979822009-09-14 19:04:10 +00001444 Elements.push_back(MakeConstantString(iter->first.second,
Chris Lattner63dd3372008-06-26 04:10:42 +00001445 ".objc_sel_types"));
Owen Anderson0e0189d2009-07-27 22:29:56 +00001446 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001447 Elements.clear();
1448 }
1449 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1450 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner63dd3372008-06-26 04:10:42 +00001451 iter != iterEnd; ++iter) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001452 Elements.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001453 ExportUniqueString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001454 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001455 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001456 Elements.clear();
1457 }
1458 Elements.push_back(NULLPtr);
1459 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001460 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001461 Elements.clear();
1462 // Number of static selectors
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001463 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001464 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson9793f0e2009-07-29 22:16:19 +00001465 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001466 ".objc_selector_list");
Mike Stump11289f42009-09-09 15:08:12 +00001467 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001468 SelStructPtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001469
1470 // Now that all of the static selectors exist, create pointers to them.
1471 int index = 0;
David Chisnall92b762e2010-02-03 02:09:30 +00001472 llvm::SmallVector<std::pair<llvm::GlobalAlias*,llvm::Value*>, 16> selectors;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001473 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1474 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1475 iter != iterEnd; ++iter) {
1476 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001477 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
David Chisnall92b762e2010-02-03 02:09:30 +00001478 llvm::Constant *SelPtr =
1479 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001480 // If selectors are defined as an opaque type, cast the pointer to this
1481 // type.
1482 if (isSelOpaque) {
David Chisnall92b762e2010-02-03 02:09:30 +00001483 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,SelectorTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001484 }
David Chisnall92b762e2010-02-03 02:09:30 +00001485 selectors.push_back(
1486 std::pair<llvm::GlobalAlias*,llvm::Value*>((*iter).second, SelPtr));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001487 }
1488 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1489 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1490 iter != iterEnd; iter++) {
1491 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001492 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
David Chisnall92b762e2010-02-03 02:09:30 +00001493 llvm::Constant *SelPtr =
1494 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001495 // If selectors are defined as an opaque type, cast the pointer to this
1496 // type.
1497 if (isSelOpaque) {
David Chisnall92b762e2010-02-03 02:09:30 +00001498 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001499 }
David Chisnall92b762e2010-02-03 02:09:30 +00001500 selectors.push_back(
1501 std::pair<llvm::GlobalAlias*,llvm::Value*>((*iter).second, SelPtr));
1502 }
1503 for (llvm::SmallVectorImpl<std::pair<
1504 llvm::GlobalAlias*,llvm::Value*> >::iterator
1505 iter=selectors.begin(), iterEnd =selectors.end();
1506 iter != iterEnd; ++iter) {
1507 iter->first->replaceAllUsesWith(iter->second);
1508 iter->first->eraseFromParent();
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001509 }
1510 // Number of classes defined.
Mike Stump11289f42009-09-09 15:08:12 +00001511 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001512 Classes.size()));
1513 // Number of categories defined
Mike Stump11289f42009-09-09 15:08:12 +00001514 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001515 Categories.size()));
1516 // Create an array of classes, then categories, then static object instances
1517 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1518 // NULL-terminated list of static object instances (mainly constant strings)
1519 Classes.push_back(Statics);
1520 Classes.push_back(NULLPtr);
Owen Anderson47034e12009-07-28 18:33:04 +00001521 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001522 Elements.push_back(ClassList);
Mike Stump11289f42009-09-09 15:08:12 +00001523 // Construct the symbol table
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001524 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1525
1526 // The symbol table is contained in a module which has some version-checking
1527 // constants
Owen Anderson758428f2009-08-05 23:18:46 +00001528 llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001529 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001530 Elements.clear();
1531 // Runtime version used for compatibility checking.
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001532 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Mike Stump11289f42009-09-09 15:08:12 +00001533 Elements.push_back(llvm::ConstantInt::get(LongTy,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001534 NonFragileRuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001535 } else {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001536 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001537 }
Fariborz Jahanianc2d56182009-04-01 19:49:42 +00001538 // sizeof(ModuleTy)
1539 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001540 Elements.push_back(llvm::ConstantInt::get(LongTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001541 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001542 //FIXME: Should be the path to the file where this module was declared
1543 Elements.push_back(NULLPtr);
1544 Elements.push_back(SymTab);
1545 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1546
1547 // Create the load function calling the runtime entry point with the module
1548 // structure
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001549 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson41a75022009-08-13 21:57:51 +00001550 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001551 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1552 &TheModule);
Owen Anderson41a75022009-08-13 21:57:51 +00001553 llvm::BasicBlock *EntryBB =
1554 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Anderson170229f2009-07-14 23:10:40 +00001555 CGBuilderTy Builder(VMContext);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001556 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian3b636c12009-03-30 18:02:14 +00001557
1558 std::vector<const llvm::Type*> Params(1,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001559 llvm::PointerType::getUnqual(ModuleTy));
1560 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +00001561 llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001562 Builder.CreateCall(Register, Module);
1563 Builder.CreateRetVoid();
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001564
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001565 return LoadFunction;
1566}
Daniel Dunbar92992502008-08-15 22:20:32 +00001567
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00001568llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump11289f42009-09-09 15:08:12 +00001569 const ObjCContainerDecl *CD) {
1570 const ObjCCategoryImplDecl *OCD =
Steve Naroff11b387f2009-01-08 19:41:02 +00001571 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattnere4b95692008-11-24 03:33:13 +00001572 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1573 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1574 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorffca3a22009-01-09 17:18:27 +00001575 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar92992502008-08-15 22:20:32 +00001576
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001577 CodeGenTypes &Types = CGM.getTypes();
Mike Stump11289f42009-09-09 15:08:12 +00001578 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001579 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001580 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1581 MethodName, isClassMethod);
1582
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001583 llvm::Function *Method
Mike Stump11289f42009-09-09 15:08:12 +00001584 = llvm::Function::Create(MethodTy,
1585 llvm::GlobalValue::InternalLinkage,
1586 FunctionName,
1587 &TheModule);
Chris Lattner4bd55962008-03-30 23:03:07 +00001588 return Method;
1589}
1590
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001591llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Mike Stump11289f42009-09-09 15:08:12 +00001592 std::vector<const llvm::Type*> Params;
1593 const llvm::Type *BoolTy =
1594 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1595 Params.push_back(IdTy);
1596 Params.push_back(SelectorTy);
1597 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1598 Params.push_back(LongTy);
1599 Params.push_back(BoolTy);
1600 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1601 const llvm::FunctionType *FTy =
1602 llvm::FunctionType::get(IdTy, Params, false);
1603 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1604 "objc_getProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001605}
1606
1607llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Mike Stump11289f42009-09-09 15:08:12 +00001608 std::vector<const llvm::Type*> Params;
1609 const llvm::Type *BoolTy =
1610 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1611 Params.push_back(IdTy);
1612 Params.push_back(SelectorTy);
1613 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1614 Params.push_back(LongTy);
1615 Params.push_back(IdTy);
1616 Params.push_back(BoolTy);
1617 Params.push_back(BoolTy);
1618 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1619 const llvm::FunctionType *FTy =
1620 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
1621 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1622 "objc_setProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001623}
1624
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001625llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
1626 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1627 ASTContext &Ctx = CGM.getContext();
1628 // void objc_enumerationMutation (id)
1629 llvm::SmallVector<QualType,16> Params;
David Chisnall9f57c292009-08-17 16:35:33 +00001630 Params.push_back(ASTIdTy);
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001631 const llvm::FunctionType *FTy =
1632 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
1633 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
Anders Carlsson3f35a262008-08-31 04:05:03 +00001634}
1635
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00001636void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1637 const Stmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001638 // Pointer to the personality function
1639 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +00001640 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +00001641 true),
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001642 "__gnu_objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +00001643 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001644 std::vector<const llvm::Type*> Params;
1645 Params.push_back(PtrTy);
1646 llvm::Value *RethrowFn =
Owen Anderson41a75022009-08-13 21:57:51 +00001647 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
David Chisnall9a2073c2010-01-06 18:02:59 +00001648 Params, false), "_Unwind_Resume");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001649
1650 bool isTry = isa<ObjCAtTryStmt>(S);
1651 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1652 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1653 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattner9fea9442009-05-11 18:16:28 +00001654 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001655 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1656 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1657 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1658
David Chisnall3a509cd2009-12-24 02:26:34 +00001659 // @synchronized()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001660 if (!isTry) {
Chris Lattner9fea9442009-05-11 18:16:28 +00001661 std::vector<const llvm::Type*> Args(1, IdTy);
1662 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001663 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001664 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
Mike Stump11289f42009-09-09 15:08:12 +00001665 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001666 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1667 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1668 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001669 }
1670
Chris Lattner9fea9442009-05-11 18:16:28 +00001671
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001672 // Push an EH context entry, used for handling rethrows and jumps
1673 // through finally.
1674 CGF.PushCleanupBlock(FinallyBlock);
1675
1676 // Emit the statements in the @try {} block
1677 CGF.setInvokeDest(TryHandler);
1678
1679 CGF.EmitBlock(TryBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001680 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001681 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1682
1683 // Jump to @finally if there is no exception
1684 CGF.EmitBranchThroughCleanup(FinallyEnd);
1685
1686 // Emit the handlers
1687 CGF.EmitBlock(TryHandler);
1688
Chris Lattner96afab52009-05-08 17:36:08 +00001689 // Get the correct versions of the exception handling intrinsics
1690 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Mike Stump11289f42009-09-09 15:08:12 +00001691 llvm::Value *llvm_eh_exception =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001692 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Duncan Sandscef56992009-10-14 16:13:30 +00001693 llvm::Value *llvm_eh_selector =
1694 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
1695 llvm::Value *llvm_eh_typeid_for =
1696 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001697
1698 // Exception object
1699 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1700 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1701
1702 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1703 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1704
1705 ESelArgs.push_back(Exc);
1706 ESelArgs.push_back(Personality);
1707
1708 bool HasCatchAll = false;
1709 // Only @try blocks are allowed @catch blocks, but both can have @finally
1710 if (isTry) {
1711 if (const ObjCAtCatchStmt* CatchStmt =
1712 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattner9fea9442009-05-11 18:16:28 +00001713 CGF.setInvokeDest(CatchInCatch);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001714
1715 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1716 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Mike Stumpdd93a192009-07-31 21:31:32 +00001717 Handlers.push_back(std::make_pair(CatchDecl,
1718 CatchStmt->getCatchBody()));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001719
1720 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff7cae42b2009-07-10 23:34:53 +00001721 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001722 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1723 // Use i8* null here to signal this is a catch all, not a cleanup.
1724 ESelArgs.push_back(NULLPtr);
1725 HasCatchAll = true;
1726 // No further catches after this one will ever by reached
1727 break;
Mike Stump11289f42009-09-09 15:08:12 +00001728 }
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001729
1730 // All other types should be Objective-C interface pointer types.
Mike Stump11289f42009-09-09 15:08:12 +00001731 const ObjCObjectPointerType *OPT =
John McCall9dd450b2009-09-21 23:43:11 +00001732 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001733 assert(OPT && "Invalid @catch type.");
Mike Stump11289f42009-09-09 15:08:12 +00001734 const ObjCInterfaceType *IT =
John McCall9dd450b2009-09-21 23:43:11 +00001735 OPT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001736 assert(IT && "Invalid @catch type.");
Chris Lattner96afab52009-05-08 17:36:08 +00001737 llvm::Value *EHType =
1738 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001739 ESelArgs.push_back(EHType);
1740 }
1741 }
1742 }
1743
1744 // We use a cleanup unless there was already a catch all.
1745 if (!HasCatchAll) {
Owen Anderson41a75022009-08-13 21:57:51 +00001746 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001747 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1748 }
1749
1750 // Find which handler was matched.
Chris Lattner96afab52009-05-08 17:36:08 +00001751 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001752 ESelArgs.begin(), ESelArgs.end(), "selector");
1753
1754 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1755 const ParmVarDecl *CatchParam = Handlers[i].first;
1756 const Stmt *CatchBody = Handlers[i].second;
1757
1758 llvm::BasicBlock *Next = 0;
1759
1760 // The last handler always matches.
1761 if (i + 1 != e) {
1762 assert(CatchParam && "Only last handler can be a catch all.");
1763
1764 // Test whether this block matches the type for the selector and branch
1765 // to Match if it does, or to the next BB if it doesn't.
1766 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1767 Next = CGF.createBasicBlock("catch.next");
Chris Lattner96afab52009-05-08 17:36:08 +00001768 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001769 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1770 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1771 Next);
1772
1773 CGF.EmitBlock(Match);
1774 }
Mike Stump11289f42009-09-09 15:08:12 +00001775
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001776 if (CatchBody) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001777 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1778 CGF.ConvertType(CatchParam->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001779
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001780 // Bind the catch parameter if it exists.
1781 if (CatchParam) {
1782 // CatchParam is a ParmVarDecl because of the grammar
1783 // construction used to handle this, but for codegen purposes
1784 // we treat this as a local decl.
1785 CGF.EmitLocalBlockVarDecl(*CatchParam);
1786 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1787 }
1788
1789 CGF.ObjCEHValueStack.push_back(ExcObject);
1790 CGF.EmitStmt(CatchBody);
1791 CGF.ObjCEHValueStack.pop_back();
1792
1793 CGF.EmitBranchThroughCleanup(FinallyEnd);
1794
1795 if (Next)
1796 CGF.EmitBlock(Next);
1797 } else {
1798 assert(!Next && "catchup should be last handler.");
1799
1800 CGF.Builder.CreateStore(Exc, RethrowPtr);
1801 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1802 }
1803 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001804 // The @finally block is a secondary landing pad for any exceptions thrown in
1805 // @catch() blocks
1806 CGF.EmitBlock(CatchInCatch);
1807 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1808 ESelArgs.clear();
1809 ESelArgs.push_back(Exc);
1810 ESelArgs.push_back(Personality);
David Chisnall3a509cd2009-12-24 02:26:34 +00001811 // If there is a @catch or @finally clause in outside of this one then we
1812 // need to make sure that we catch and rethrow it.
1813 if (PrevLandingPad) {
1814 ESelArgs.push_back(NULLPtr);
1815 } else {
1816 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
1817 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001818 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1819 "selector");
1820 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1821 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1822 CGF.Builder.CreateStore(Exc, RethrowPtr);
1823 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1824
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001825 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1826
1827 CGF.setInvokeDest(PrevLandingPad);
1828
1829 CGF.EmitBlock(FinallyBlock);
1830
Chris Lattner9fea9442009-05-11 18:16:28 +00001831
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001832 if (isTry) {
Mike Stump11289f42009-09-09 15:08:12 +00001833 if (const ObjCAtFinallyStmt* FinallyStmt =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001834 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1835 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1836 } else {
Chris Lattner9fea9442009-05-11 18:16:28 +00001837 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001838 // @synchronized.
Chris Lattner9fea9442009-05-11 18:16:28 +00001839 std::vector<const llvm::Type*> Args(1, IdTy);
1840 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001841 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001842 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Mike Stump11289f42009-09-09 15:08:12 +00001843 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001844 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1845 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1846 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001847 }
1848
1849 if (Info.SwitchBlock)
1850 CGF.EmitBlock(Info.SwitchBlock);
1851 if (Info.EndBlock)
1852 CGF.EmitBlock(Info.EndBlock);
1853
1854 // Branch around the rethrow code.
1855 CGF.EmitBranch(FinallyEnd);
1856
1857 CGF.EmitBlock(FinallyRethrow);
David Chisnall3a509cd2009-12-24 02:26:34 +00001858
1859 llvm::Value *ExceptionObject = CGF.Builder.CreateLoad(RethrowPtr);
1860 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1861 if (!UnwindBB) {
1862 CGF.Builder.CreateCall(RethrowFn, ExceptionObject);
1863 // Exception always thrown, next instruction is never reached.
1864 CGF.Builder.CreateUnreachable();
1865 } else {
1866 // If there is a @catch block outside this scope, we invoke instead of
1867 // calling because we may return to this function. This is very slow, but
1868 // some people still do it. It would be nice to add an optimised path for
1869 // this.
1870 CGF.Builder.CreateInvoke(RethrowFn, UnwindBB, UnwindBB, &ExceptionObject,
1871 &ExceptionObject+1);
1872 }
Mike Stump11289f42009-09-09 15:08:12 +00001873
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001874 CGF.EmitBlock(FinallyEnd);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001875}
1876
1877void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001878 const ObjCAtThrowStmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001879 llvm::Value *ExceptionAsObject;
1880
1881 std::vector<const llvm::Type*> Args(1, IdTy);
1882 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001883 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Mike Stump11289f42009-09-09 15:08:12 +00001884 llvm::Value *ThrowFn =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001885 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Mike Stump11289f42009-09-09 15:08:12 +00001886
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001887 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1888 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian078cd522009-05-17 16:49:27 +00001889 ExceptionAsObject = Exception;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001890 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001891 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001892 "Unexpected rethrow outside @catch block.");
1893 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1894 }
Fariborz Jahanian078cd522009-05-17 16:49:27 +00001895 ExceptionAsObject =
1896 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +00001897
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001898 // Note: This may have to be an invoke, if we want to support constructs like:
1899 // @try {
1900 // @throw(obj);
1901 // }
1902 // @catch(id) ...
1903 //
1904 // This is effectively turning @throw into an incredibly-expensive goto, but
1905 // it may happen as a result of inlining followed by missed optimizations, or
1906 // as a result of stupidity.
1907 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1908 if (!UnwindBB) {
1909 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1910 CGF.Builder.CreateUnreachable();
1911 } else {
1912 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1913 &ExceptionAsObject+1);
1914 }
1915 // Clear the insertion point to indicate we are in unreachable code.
1916 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001917}
1918
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001919llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001920 llvm::Value *AddrWeakObj) {
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00001921 return 0;
1922}
1923
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001924void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001925 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001926 return;
1927}
1928
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001929void CGObjCGNU::EmitObjCGlobalAssign(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 Jahaniane881b532008-11-20 19:23:36 +00001934void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001935 llvm::Value *src, llvm::Value *dst,
1936 llvm::Value *ivarOffset) {
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001937 return;
1938}
1939
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001940void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001941 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001942 return;
1943}
1944
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001945void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00001946 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001947 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +00001948 QualType Ty) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001949 return;
1950}
1951
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001952llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1953 const ObjCInterfaceDecl *ID,
1954 const ObjCIvarDecl *Ivar) {
1955 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1956 + '.' + Ivar->getNameAsString();
1957 // Emit the variable and initialize it with what we think the correct value
1958 // is. This allows code compiled with non-fragile ivars to work correctly
1959 // when linked against code which isn't (most of the time).
David Chisnall5778fce2009-08-31 16:41:57 +00001960 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1961 if (!IvarOffsetPointer) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001962 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1963 llvm::ConstantInt *OffsetGuess =
David Chisnallc8fc5732010-01-11 19:02:35 +00001964 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
David Chisnall5778fce2009-08-31 16:41:57 +00001965 // Don't emit the guess in non-PIC code because the linker will not be able
1966 // to replace it with the real version for a library. In non-PIC code you
1967 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump11289f42009-09-09 15:08:12 +00001968 // GCC-compiled class.
David Chisnall5778fce2009-08-31 16:41:57 +00001969 if (CGM.getLangOptions().PICLevel) {
1970 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
1971 llvm::Type::getInt32Ty(VMContext), false,
1972 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
1973 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
1974 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
1975 IvarOffsetGV, Name);
1976 } else {
1977 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001978 llvm::Type::getInt32PtrTy(VMContext), false,
1979 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall5778fce2009-08-31 16:41:57 +00001980 }
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001981 }
David Chisnall5778fce2009-08-31 16:41:57 +00001982 return IvarOffsetPointer;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001983}
1984
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001985LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1986 QualType ObjectTy,
1987 llvm::Value *BaseValue,
1988 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001989 unsigned CVRQualifiers) {
John McCall9dd450b2009-09-21 23:43:11 +00001990 const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00001991 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1992 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00001993}
Mike Stumpdd93a192009-07-31 21:31:32 +00001994
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001995static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1996 const ObjCInterfaceDecl *OID,
1997 const ObjCIvarDecl *OIVD) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001998 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001999 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002000 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
2001 if (OIVD == Ivars[k])
2002 return OID;
2003 }
Mike Stump11289f42009-09-09 15:08:12 +00002004
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002005 // Otherwise check in the super class.
2006 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2007 return FindIvarInterface(Context, Super, OIVD);
Mike Stump11289f42009-09-09 15:08:12 +00002008
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002009 return 0;
2010}
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002011
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002012llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00002013 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002014 const ObjCIvarDecl *Ivar) {
David Chisnall5778fce2009-08-31 16:41:57 +00002015 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002016 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall5778fce2009-08-31 16:41:57 +00002017 return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2018 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar"));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002019 }
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002020 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002021 return llvm::ConstantInt::get(LongTy, Offset, "ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002022}
2023
Mike Stumpdd93a192009-07-31 21:31:32 +00002024CodeGen::CGObjCRuntime *
2025CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) {
Chris Lattner87ab27d2008-06-26 04:19:03 +00002026 return new CGObjCGNU(CGM);
Chris Lattnerb7256cd2008-03-01 08:50:34 +00002027}