blob: 123106f9858ca2387fb31501ec24e0e3d6bda3f9 [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 Chisnall5bb4efd2010-02-03 15:59:02 +000058 const llvm::PointerType *PtrToIdTy;
John McCall2da83a32010-02-26 00:48:12 +000059 CanQualType ASTIdTy;
Chris Lattner8d3f4a42009-01-27 05:06:01 +000060 const llvm::IntegerType *IntTy;
61 const llvm::PointerType *PtrTy;
62 const llvm::IntegerType *LongTy;
63 const llvm::PointerType *PtrToIntTy;
Daniel Dunbar566421c2009-05-04 15:31:17 +000064 llvm::GlobalAlias *ClassPtrAlias;
65 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000066 std::vector<llvm::Constant*> Classes;
67 std::vector<llvm::Constant*> Categories;
68 std::vector<llvm::Constant*> ConstantStrings;
David Chisnall358e7512010-01-27 12:49:23 +000069 llvm::StringMap<llvm::Constant*> ObjCStrings;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000070 llvm::Function *LoadFunction;
71 llvm::StringMap<llvm::Constant*> ExistingProtocols;
72 typedef std::pair<std::string, std::string> TypedSelector;
73 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
74 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
David Chisnall5bb4efd2010-02-03 15:59:02 +000075 // Selectors that we don't emit in GC mode
76 Selector RetainSel, ReleaseSel, AutoreleaseSel;
77 // Functions used for GC.
78 llvm::Constant *IvarAssignFn, *StrongCastAssignFn, *MemMoveFn, *WeakReadFn,
79 *WeakAssignFn, *GlobalAssignFn;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000080 // Some zeros used for GEPs in lots of places.
81 llvm::Constant *Zeros[2];
82 llvm::Constant *NULLPtr;
Owen Anderson170229f2009-07-14 23:10:40 +000083 llvm::LLVMContext &VMContext;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000084private:
85 llvm::Constant *GenerateIvarList(
86 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
87 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
88 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
89 llvm::Constant *GenerateMethodList(const std::string &ClassName,
90 const std::string &CategoryName,
Mike Stump11289f42009-09-09 15:08:12 +000091 const llvm::SmallVectorImpl<Selector> &MethodSels,
92 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000093 bool isClassMethodList);
Fariborz Jahanian89d23972009-03-31 18:27:22 +000094 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +000095 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
96 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
97 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000098 llvm::Constant *GenerateProtocolList(
99 const llvm::SmallVectorImpl<std::string> &Protocols);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000100 // To ensure that all protocols are seen by the runtime, we add a category on
101 // a class defined in the runtime, declaring no methods, but adopting the
102 // protocols.
103 void GenerateProtocolHolderCategory(void);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000104 llvm::Constant *GenerateClassStructure(
105 llvm::Constant *MetaClass,
106 llvm::Constant *SuperClass,
107 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000108 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000109 llvm::Constant *Version,
110 llvm::Constant *InstanceSize,
111 llvm::Constant *IVars,
112 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000113 llvm::Constant *Protocols,
114 llvm::Constant *IvarOffsets,
David Chisnalld472c852010-04-28 14:29:56 +0000115 llvm::Constant *Properties,
116 bool isMeta=false);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000117 llvm::Constant *GenerateProtocolMethodList(
118 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
119 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
120 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
121 &Name="");
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000122 llvm::Constant *ExportUniqueString(const std::string &Str, const std::string
123 prefix);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000124 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
Benjamin Kramerf3a499a2010-02-09 19:31:24 +0000125 std::vector<llvm::Constant*> &V, llvm::StringRef Name="",
David Chisnalldf349172010-01-08 00:14:31 +0000126 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000127 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
Benjamin Kramerf3a499a2010-02-09 19:31:24 +0000128 std::vector<llvm::Constant*> &V, llvm::StringRef Name="",
David Chisnalldf349172010-01-08 00:14:31 +0000129 llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage);
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +0000130 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
131 const ObjCIvarDecl *Ivar);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000132 void EmitClassRef(const std::string &className);
David Chisnall5bb4efd2010-02-03 15:59:02 +0000133 llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, const llvm::Type *Ty){
134 if (V->getType() == Ty) return V;
135 return B.CreateBitCast(V, Ty);
136 }
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000137public:
Chris Lattner87ab27d2008-06-26 04:19:03 +0000138 CGObjCGNU(CodeGen::CodeGenModule &cgm);
David Chisnall481e3a82010-01-23 02:40:42 +0000139 virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
Mike Stump11289f42009-09-09 15:08:12 +0000140 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000141 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000142 QualType ResultType,
143 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000144 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000145 bool IsClassMessage,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000146 const CallArgList &CallArgs,
147 const ObjCMethodDecl *Method);
Mike Stump11289f42009-09-09 15:08:12 +0000148 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000149 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000150 QualType ResultType,
151 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000152 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000153 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000154 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000155 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000156 const CallArgList &CallArgs,
157 const ObjCMethodDecl *Method);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000158 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000159 const ObjCInterfaceDecl *OID);
Daniel Dunbar45858d22010-02-03 20:11:42 +0000160 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
161 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
162 *Method);
Mike Stump11289f42009-09-09 15:08:12 +0000163
164 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +0000165 const ObjCContainerDecl *CD);
Daniel Dunbar92992502008-08-15 22:20:32 +0000166 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
167 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarcb463852008-11-01 01:53:16 +0000168 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000169 const ObjCProtocolDecl *PD);
170 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000171 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbara91c3e02008-09-24 03:38:44 +0000172 virtual llvm::Function *GetPropertyGetFunction();
173 virtual llvm::Function *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000174 virtual llvm::Function *GetCopyStructFunction();
Daniel Dunbarc46a0792009-07-24 07:40:24 +0000175 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump11289f42009-09-09 15:08:12 +0000176
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +0000177 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
178 const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +0000179 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
180 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000181 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000182 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +0000183 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
184 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000185 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
186 llvm::Value *src, llvm::Value *dest);
Fariborz Jahaniane881b532008-11-20 19:23:36 +0000187 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000188 llvm::Value *src, llvm::Value *dest,
189 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000190 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
191 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000192 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +0000193 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000194 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000195 QualType Ty);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000196 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
197 QualType ObjectTy,
198 llvm::Value *BaseValue,
199 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +0000200 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000201 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +0000202 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +0000203 const ObjCIvarDecl *Ivar);
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000204};
205} // end anonymous namespace
206
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000207
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000208/// Emits a reference to a dummy variable which is emitted with each class.
209/// This ensures that a linker error will be generated when trying to link
210/// together modules where a referenced class is not defined.
Mike Stumpdd93a192009-07-31 21:31:32 +0000211void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000212 std::string symbolRef = "__objc_class_ref_" + className;
213 // Don't emit two copies of the same symbol
Mike Stumpdd93a192009-07-31 21:31:32 +0000214 if (TheModule.getGlobalVariable(symbolRef))
215 return;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000216 std::string symbolName = "__objc_class_name_" + className;
217 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
218 if (!ClassSymbol) {
Owen Andersonc10c8d32009-07-08 19:05:04 +0000219 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
220 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000221 }
Owen Andersonc10c8d32009-07-08 19:05:04 +0000222 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerc58e5692009-08-05 05:25:18 +0000223 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000224}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000225
226static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000227 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000228}
229
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000230static std::string SymbolNameForMethod(const std::string &ClassName, const
231 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
232{
David Chisnall035ead22010-01-14 14:08:19 +0000233 std::string MethodNameColonStripped = MethodName;
234 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
235 ':', '_');
236 return std::string(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
237 CategoryName + "_" + MethodNameColonStripped;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000238}
239
Chris Lattner87ab27d2008-06-26 04:19:03 +0000240CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar566421c2009-05-04 15:31:17 +0000241 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Anderson170229f2009-07-14 23:10:40 +0000242 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000243 IntTy = cast<llvm::IntegerType>(
244 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
245 LongTy = cast<llvm::IntegerType>(
246 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Mike Stump11289f42009-09-09 15:08:12 +0000247
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000248 Int8Ty = llvm::Type::getInt8Ty(VMContext);
249 // C string type. Used in lots of places.
250 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
251
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000252 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000253 Zeros[1] = Zeros[0];
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000254 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner4bd55962008-03-30 23:03:07 +0000255 // Get the selector Type.
David Chisnall481e3a82010-01-23 02:40:42 +0000256 QualType selTy = CGM.getContext().getObjCSelType();
257 if (QualType() == selTy) {
258 SelectorTy = PtrToInt8Ty;
259 } else {
260 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
261 }
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000262
Owen Anderson9793f0e2009-07-29 22:16:19 +0000263 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner4bd55962008-03-30 23:03:07 +0000264 PtrTy = PtrToInt8Ty;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner4bd55962008-03-30 23:03:07 +0000266 // Object type
John McCall2da83a32010-02-26 00:48:12 +0000267 ASTIdTy = CGM.getContext().getCanonicalType(CGM.getContext().getObjCIdType());
David Chisnall481e3a82010-01-23 02:40:42 +0000268 if (QualType() == ASTIdTy) {
269 IdTy = PtrToInt8Ty;
270 } else {
271 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
272 }
David Chisnall5bb4efd2010-02-03 15:59:02 +0000273 PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner4bd55962008-03-30 23:03:07 +0000275 // IMP type
276 std::vector<const llvm::Type*> IMPArgs;
277 IMPArgs.push_back(IdTy);
278 IMPArgs.push_back(SelectorTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000279 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
David Chisnall5bb4efd2010-02-03 15:59:02 +0000280
281 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
282 // Get selectors needed in GC mode
283 RetainSel = GetNullarySelector("retain", CGM.getContext());
284 ReleaseSel = GetNullarySelector("release", CGM.getContext());
285 AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
286
287 // Get functions needed in GC mode
288
289 // id objc_assign_ivar(id, id, ptrdiff_t);
290 std::vector<const llvm::Type*> Args(1, IdTy);
291 Args.push_back(PtrToIdTy);
292 // FIXME: ptrdiff_t
293 Args.push_back(LongTy);
294 llvm::FunctionType *FTy = llvm::FunctionType::get(IdTy, Args, false);
295 IvarAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
296 // id objc_assign_strongCast (id, id*)
297 Args.pop_back();
298 FTy = llvm::FunctionType::get(IdTy, Args, false);
299 StrongCastAssignFn =
300 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
301 // id objc_assign_global(id, id*);
302 FTy = llvm::FunctionType::get(IdTy, Args, false);
303 GlobalAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
304 // id objc_assign_weak(id, id*);
305 FTy = llvm::FunctionType::get(IdTy, Args, false);
306 WeakAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
307 // id objc_read_weak(id*);
308 Args.clear();
309 Args.push_back(PtrToIdTy);
310 FTy = llvm::FunctionType::get(IdTy, Args, false);
311 WeakReadFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
312 // void *objc_memmove_collectable(void*, void *, size_t);
313 Args.clear();
314 Args.push_back(PtrToInt8Ty);
315 Args.push_back(PtrToInt8Ty);
316 // FIXME: size_t
317 Args.push_back(LongTy);
318 FTy = llvm::FunctionType::get(IdTy, Args, false);
319 MemMoveFn = CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
320 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000321}
Mike Stumpdd93a192009-07-31 21:31:32 +0000322
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000323// This has to perform the lookup every time, since posing and related
324// techniques can modify the name -> class mapping.
Daniel Dunbarcb463852008-11-01 01:53:16 +0000325llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000326 const ObjCInterfaceDecl *OID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000327 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
David Chisnalldf349172010-01-08 00:14:31 +0000328 // With the incompatible ABI, this will need to be replaced with a direct
329 // reference to the class symbol. For the compatible nonfragile ABI we are
330 // still performing this lookup at run time but emitting the symbol for the
331 // class externally so that we can make the switch later.
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000332 EmitClassRef(OID->getNameAsString());
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +0000333 ClassName = Builder.CreateStructGEP(ClassName, 0);
334
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000335 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000336 llvm::Constant *ClassLookupFn =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000337 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000338 Params,
339 true),
340 "objc_lookup_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000341 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner4bd55962008-03-30 23:03:07 +0000342}
343
Daniel Dunbar45858d22010-02-03 20:11:42 +0000344llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnere4b95692008-11-24 03:33:13 +0000345 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner6d522c02008-06-26 04:37:12 +0000346 if (US == 0)
Daniel Dunbar45858d22010-02-03 20:11:42 +0000347 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
David Chisnall9f57c292009-08-17 16:35:33 +0000348 llvm::GlobalValue::PrivateLinkage,
349 ".objc_untyped_selector_alias"+Sel.getAsString(),
Chris Lattner6d522c02008-06-26 04:37:12 +0000350 NULL, &TheModule);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Daniel Dunbar45858d22010-02-03 20:11:42 +0000352 return Builder.CreateLoad(US);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000353}
354
Daniel Dunbar45858d22010-02-03 20:11:42 +0000355llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000356 *Method) {
357
358 std::string SelName = Method->getSelector().getAsString();
359 std::string SelTypes;
360 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
361 // Typed selectors
362 TypedSelector Selector = TypedSelector(SelName,
363 SelTypes);
364
365 // If it's already cached, return it.
Mike Stumpdd93a192009-07-31 21:31:32 +0000366 if (TypedSelectors[Selector]) {
Daniel Dunbar45858d22010-02-03 20:11:42 +0000367 return Builder.CreateLoad(TypedSelectors[Selector]);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000368 }
369
370 // If it isn't, cache it.
371 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Daniel Dunbar45858d22010-02-03 20:11:42 +0000372 llvm::PointerType::getUnqual(SelectorTy),
David Chisnall9f57c292009-08-17 16:35:33 +0000373 llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000374 NULL, &TheModule);
375 TypedSelectors[Selector] = Sel;
376
Daniel Dunbar45858d22010-02-03 20:11:42 +0000377 return Builder.CreateLoad(Sel);
Chris Lattner6d522c02008-06-26 04:37:12 +0000378}
379
Chris Lattnerd9b98862008-06-26 04:44:19 +0000380llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
381 const std::string &Name) {
David Chisnall5778fce2009-08-31 16:41:57 +0000382 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Owen Andersonade90fd2009-07-29 18:54:39 +0000383 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000384}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000385llvm::Constant *CGObjCGNU::ExportUniqueString(const std::string &Str,
386 const std::string prefix) {
387 std::string name = prefix + Str;
388 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
389 if (!ConstStr) {
390 llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
391 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
392 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
393 }
394 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
395}
Mike Stumpdd93a192009-07-31 21:31:32 +0000396
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000397llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
Benjamin Kramerf3a499a2010-02-09 19:31:24 +0000398 std::vector<llvm::Constant*> &V, llvm::StringRef Name,
David Chisnalldf349172010-01-08 00:14:31 +0000399 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson0e0189d2009-07-27 22:29:56 +0000400 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000401 return new llvm::GlobalVariable(TheModule, Ty, false,
402 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000403}
Mike Stumpdd93a192009-07-31 21:31:32 +0000404
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000405llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
Benjamin Kramerf3a499a2010-02-09 19:31:24 +0000406 std::vector<llvm::Constant*> &V, llvm::StringRef Name,
David Chisnalldf349172010-01-08 00:14:31 +0000407 llvm::GlobalValue::LinkageTypes linkage) {
Owen Anderson47034e12009-07-28 18:33:04 +0000408 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000409 return new llvm::GlobalVariable(TheModule, Ty, false,
Mike Stumpdd93a192009-07-31 21:31:32 +0000410 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000411}
412
413/// Generate an NSConstantString object.
David Chisnall481e3a82010-01-23 02:40:42 +0000414llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall358e7512010-01-27 12:49:23 +0000415
David Chisnall481e3a82010-01-23 02:40:42 +0000416 std::string Str(SL->getStrData(), SL->getByteLength());
417
David Chisnall358e7512010-01-27 12:49:23 +0000418 // Look for an existing one
419 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
420 if (old != ObjCStrings.end())
421 return old->getValue();
422
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000423 std::vector<llvm::Constant*> Ivars;
424 Ivars.push_back(NULLPtr);
Chris Lattner091f6982008-06-21 21:44:18 +0000425 Ivars.push_back(MakeConstantString(Str));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000426 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000427 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson758428f2009-08-05 23:18:46 +0000428 llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000429 Ivars, ".objc_str");
David Chisnall358e7512010-01-27 12:49:23 +0000430 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
431 ObjCStrings[Str] = ObjCStr;
432 ConstantStrings.push_back(ObjCStr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000433 return ObjCStr;
434}
435
436///Generates a message send where the super is the receiver. This is a message
437///send to self with special delivery semantics indicating which class's method
438///should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000439CodeGen::RValue
440CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000441 QualType ResultType,
442 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000443 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000444 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000445 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000446 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000447 const CallArgList &CallArgs,
448 const ObjCMethodDecl *Method) {
David Chisnall5bb4efd2010-02-03 15:59:02 +0000449 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
450 if (Sel == RetainSel || Sel == AutoreleaseSel) {
451 return RValue::get(Receiver);
452 }
453 if (Sel == ReleaseSel) {
454 return RValue::get(0);
455 }
456 }
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000457 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
458
459 CallArgList ActualArgs;
460
461 ActualArgs.push_back(
David Chisnall9f57c292009-08-17 16:35:33 +0000462 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
463 ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000464 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
465 CGF.getContext().getObjCSelType()));
466 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
467
468 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +0000469 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000470 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000471 const llvm::FunctionType *impType =
472 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000473
Daniel Dunbar566421c2009-05-04 15:31:17 +0000474 llvm::Value *ReceiverClass = 0;
Chris Lattnera02cb802009-05-08 15:39:58 +0000475 if (isCategoryImpl) {
476 llvm::Constant *classLookupFunction = 0;
477 std::vector<const llvm::Type*> Params;
478 Params.push_back(PtrTy);
479 if (IsClassMessage) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000480 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000481 IdTy, Params, true), "objc_get_meta_class");
482 } else {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000483 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattnera02cb802009-05-08 15:39:58 +0000484 IdTy, Params, true), "objc_get_class");
Daniel Dunbar566421c2009-05-04 15:31:17 +0000485 }
Chris Lattnera02cb802009-05-08 15:39:58 +0000486 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
487 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000488 } else {
Chris Lattnera02cb802009-05-08 15:39:58 +0000489 // Set up global aliases for the metaclass or class pointer if they do not
490 // already exist. These will are forward-references which will be set to
Mike Stumpdd93a192009-07-31 21:31:32 +0000491 // pointers to the class and metaclass structure created for the runtime
492 // load function. To send a message to super, we look up the value of the
Chris Lattnera02cb802009-05-08 15:39:58 +0000493 // super_class pointer from either the class or metaclass structure.
494 if (IsClassMessage) {
495 if (!MetaClassPtrAlias) {
496 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
497 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
498 Class->getNameAsString(), NULL, &TheModule);
499 }
500 ReceiverClass = MetaClassPtrAlias;
501 } else {
502 if (!ClassPtrAlias) {
503 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
504 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
505 Class->getNameAsString(), NULL, &TheModule);
506 }
507 ReceiverClass = ClassPtrAlias;
Daniel Dunbar566421c2009-05-04 15:31:17 +0000508 }
Chris Lattnerc06ce0f2009-04-25 23:19:45 +0000509 }
Daniel Dunbar566421c2009-05-04 15:31:17 +0000510 // Cast the pointer to a simplified version of the class structure
Mike Stump11289f42009-09-09 15:08:12 +0000511 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000512 llvm::PointerType::getUnqual(
Owen Anderson758428f2009-08-05 23:18:46 +0000513 llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000514 // Get the superclass pointer
515 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
516 // Load the superclass pointer
517 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000518 // Construct the structure used to look up the IMP
Owen Anderson758428f2009-08-05 23:18:46 +0000519 llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
520 Receiver->getType(), IdTy, NULL);
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000521 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000522
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000523 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar566421c2009-05-04 15:31:17 +0000524 CGF.Builder.CreateStore(ReceiverClass,
525 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000526
527 // Get the IMP
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000528 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000529 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000530 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000531 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000532 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
533 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000534 "objc_msg_lookup_super");
535
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000536 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000537 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000538 lookupArgs+2);
539
Anders Carlsson61a401c2009-12-24 19:25:24 +0000540 return CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000541}
542
Mike Stump11289f42009-09-09 15:08:12 +0000543/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000544CodeGen::RValue
545CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000546 QualType ResultType,
547 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000548 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000549 bool IsClassMessage,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000550 const CallArgList &CallArgs,
551 const ObjCMethodDecl *Method) {
David Chisnall75afda62010-04-27 15:08:48 +0000552 // Strip out message sends to retain / release in GC mode
David Chisnall5bb4efd2010-02-03 15:59:02 +0000553 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
554 if (Sel == RetainSel || Sel == AutoreleaseSel) {
555 return RValue::get(Receiver);
556 }
557 if (Sel == ReleaseSel) {
558 return RValue::get(0);
559 }
560 }
David Chisnall75afda62010-04-27 15:08:48 +0000561
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000562 CGBuilderTy &Builder = CGF.Builder;
David Chisnall75afda62010-04-27 15:08:48 +0000563
564 // If the return type is something that goes in an integer register, the
565 // runtime will handle 0 returns. For other cases, we fill in the 0 value
566 // ourselves.
567 //
568 // The language spec says the result of this kind of message send is
569 // undefined, but lots of people seem to have forgotten to read that
570 // paragraph and insist on sending messages to nil that have structure
571 // returns. With GCC, this generates a random return value (whatever happens
572 // to be on the stack / in those registers at the time) on most platforms,
573 // and generates a SegV on SPARC. With LLVM it corrupts the stack.
574 bool isPointerSizedReturn = false;
David Chisnalle6d00732010-04-27 20:33:30 +0000575 if (ResultType->isAnyPointerType() || ResultType->isIntegralType() ||
576 ResultType->isVoidType())
David Chisnall75afda62010-04-27 15:08:48 +0000577 isPointerSizedReturn = true;
578
579 llvm::BasicBlock *startBB = 0;
580 llvm::BasicBlock *messageBB = 0;
581 llvm::BasicBlock *contiueBB = 0;
582
583 if (!isPointerSizedReturn) {
584 startBB = Builder.GetInsertBlock();
585 messageBB = CGF.createBasicBlock("msgSend");
586 contiueBB = CGF.createBasicBlock("continue");
587
588 llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
589 llvm::Constant::getNullValue(Receiver->getType()));
590 Builder.CreateCondBr(isNil, contiueBB, messageBB);
591 CGF.EmitBlock(messageBB);
592 }
593
David Chisnall9f57c292009-08-17 16:35:33 +0000594 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000595 llvm::Value *cmd;
596 if (Method)
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000597 cmd = GetSelector(Builder, Method);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000598 else
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000599 cmd = GetSelector(Builder, Sel);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000600 CallArgList ActualArgs;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000601
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000602 Receiver = Builder.CreateBitCast(Receiver, IdTy);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000603 ActualArgs.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000604 std::make_pair(RValue::get(Receiver), ASTIdTy));
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000605 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
606 CGF.getContext().getObjCSelType()));
607 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
608
609 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +0000610 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000611 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +0000612 const llvm::FunctionType *impType =
613 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +0000614
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000615 llvm::Value *imp;
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000616 // For sender-aware dispatch, we pass the sender as the third argument to a
617 // lookup function. When sending messages from C code, the sender is nil.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000618 // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
619 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
620
621 std::vector<const llvm::Type*> Params;
622 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
623 Builder.CreateStore(Receiver, ReceiverPtr);
624 Params.push_back(ReceiverPtr->getType());
625 Params.push_back(SelectorTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000626 llvm::Value *self;
Fariborz Jahanian3b636c12009-03-30 18:02:14 +0000627
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000628 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
629 self = CGF.LoadObjCSelf();
630 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +0000631 self = llvm::ConstantPointerNull::get(IdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000632 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000633
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000634 Params.push_back(self->getType());
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000635
636 // The lookup function returns a slot, which can be safely cached.
637 llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy,
638 IntTy, llvm::PointerType::getUnqual(impType), NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000639 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000640 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000641 llvm::PointerType::getUnqual(SlotTy), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000642 "objc_msg_lookup_sender");
643
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000644 // The lookup function is guaranteed not to capture the receiver pointer.
645 if (llvm::Function *LookupFn = dyn_cast<llvm::Function>(lookupFunction)) {
646 LookupFn->setDoesNotCapture(1);
647 }
648
649 llvm::Value *slot =
650 Builder.CreateCall3(lookupFunction, ReceiverPtr, cmd, self);
651 imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
652 // The lookup function may have changed the receiver, so make sure we use
653 // the new one.
654 ActualArgs[0] =
655 std::make_pair(RValue::get(Builder.CreateLoad(ReceiverPtr)), ASTIdTy);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000656 } else {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000657 std::vector<const llvm::Type*> Params;
658 Params.push_back(Receiver->getType());
659 Params.push_back(SelectorTy);
Mike Stump11289f42009-09-09 15:08:12 +0000660 llvm::Constant *lookupFunction =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000661 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
662 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000663 "objc_msg_lookup");
664
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000665 imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
Fariborz Jahaniana4404f22009-05-22 20:17:16 +0000666 }
Chris Lattnerdbcc2ca2008-05-06 00:56:42 +0000667
David Chisnall75afda62010-04-27 15:08:48 +0000668 RValue msgRet =
669 CGF.EmitCall(FnInfo, imp, ReturnValueSlot(), ActualArgs);
670
671 if (!isPointerSizedReturn) {
672 CGF.EmitBlock(contiueBB);
673 if (msgRet.isScalar()) {
674 llvm::Value *v = msgRet.getScalarVal();
675 llvm::PHINode *phi = Builder.CreatePHI(v->getType());
676 phi->addIncoming(v, messageBB);
677 phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
678 msgRet = RValue::get(phi);
679 } else if (msgRet.isAggregate()) {
680 llvm::Value *v = msgRet.getAggregateAddr();
681 llvm::PHINode *phi = Builder.CreatePHI(v->getType());
682 const llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
683 llvm::AllocaInst *NullVal = CGF.CreateTempAlloca(RetTy, "null");
684 CGF.InitTempAlloca(NullVal,
685 llvm::Constant::getNullValue(RetTy->getElementType()));
686 phi->addIncoming(v, messageBB);
687 phi->addIncoming(NullVal, startBB);
688 msgRet = RValue::getAggregate(phi);
689 } else /* isComplex() */ {
690 std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
691 llvm::PHINode *phi = Builder.CreatePHI(v.first->getType());
692 phi->addIncoming(v.first, messageBB);
693 phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
694 startBB);
695 llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType());
696 phi2->addIncoming(v.second, messageBB);
697 phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
698 startBB);
699 msgRet = RValue::getComplex(phi, phi2);
700 }
701 }
702 return msgRet;
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000703}
704
Mike Stump11289f42009-09-09 15:08:12 +0000705/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000706/// objc_category structures.
707llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Mike Stump11289f42009-09-09 15:08:12 +0000708 const std::string &CategoryName,
709 const llvm::SmallVectorImpl<Selector> &MethodSels,
710 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000711 bool isClassMethodList) {
David Chisnall9f57c292009-08-17 16:35:33 +0000712 if (MethodSels.empty())
713 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000714 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000715 llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000716 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
717 PtrToInt8Ty, // Method types
Owen Anderson9793f0e2009-07-29 22:16:19 +0000718 llvm::PointerType::getUnqual(IMPTy), //Method pointer
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000719 NULL);
720 std::vector<llvm::Constant*> Methods;
721 std::vector<llvm::Constant*> Elements;
722 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
723 Elements.clear();
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000724 if (llvm::Constant *Method =
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000725 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnere4b95692008-11-24 03:33:13 +0000726 MethodSels[i].getAsString(),
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000727 isClassMethodList))) {
David Chisnall5778fce2009-08-31 16:41:57 +0000728 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
729 Elements.push_back(C);
730 Elements.push_back(MethodTypes[i]);
Owen Andersonade90fd2009-07-29 18:54:39 +0000731 Method = llvm::ConstantExpr::getBitCast(Method,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000732 llvm::PointerType::getUnqual(IMPTy));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000733 Elements.push_back(Method);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000734 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000735 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000736 }
737
738 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000739 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian078cd522009-05-17 16:49:27 +0000740 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000741 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattner882034d2008-06-26 04:52:29 +0000742 Methods);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000743
744 // Structure containing list pointer, array and array count
745 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Andersonc36edfe2009-08-13 23:27:53 +0000746 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000747 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Owen Anderson758428f2009-08-05 23:18:46 +0000748 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
Mike Stump11289f42009-09-09 15:08:12 +0000749 NextPtrTy,
750 IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000751 ObjCMethodArrayTy,
752 NULL);
753 // Refine next pointer type to concrete type
754 llvm::cast<llvm::OpaqueType>(
755 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
756 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
757
758 Methods.clear();
Owen Anderson7ec07a52009-07-30 23:11:26 +0000759 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +0000760 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson41a75022009-08-13 21:57:51 +0000761 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000762 MethodTypes.size()));
763 Methods.push_back(MethodArray);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000765 // Create an instance of the structure
766 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
767}
768
769/// Generates an IvarList. Used in construction of a objc_class.
770llvm::Constant *CGObjCGNU::GenerateIvarList(
771 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
772 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
773 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
David Chisnallb3b44ce2009-11-16 19:05:54 +0000774 if (IvarNames.size() == 0)
775 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000776 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000777 llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000778 PtrToInt8Ty,
779 PtrToInt8Ty,
780 IntTy,
781 NULL);
782 std::vector<llvm::Constant*> Ivars;
783 std::vector<llvm::Constant*> Elements;
784 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
785 Elements.clear();
David Chisnall5778fce2009-08-31 16:41:57 +0000786 Elements.push_back(IvarNames[i]);
787 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000788 Elements.push_back(IvarOffsets[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000789 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000790 }
791
792 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +0000793 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000794 IvarNames.size());
795
Mike Stump11289f42009-09-09 15:08:12 +0000796
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000797 Elements.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000798 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson47034e12009-07-28 18:33:04 +0000799 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000800 // Structure containing array and array count
Owen Anderson758428f2009-08-05 23:18:46 +0000801 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000802 ObjCIvarArrayTy,
803 NULL);
804
805 // Create an instance of the structure
806 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
807}
808
809/// Generate a class structure
810llvm::Constant *CGObjCGNU::GenerateClassStructure(
811 llvm::Constant *MetaClass,
812 llvm::Constant *SuperClass,
813 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000814 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000815 llvm::Constant *Version,
816 llvm::Constant *InstanceSize,
817 llvm::Constant *IVars,
818 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000819 llvm::Constant *Protocols,
820 llvm::Constant *IvarOffsets,
David Chisnalld472c852010-04-28 14:29:56 +0000821 llvm::Constant *Properties,
822 bool isMeta) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000823 // Set up the class structure
824 // Note: Several of these are char*s when they should be ids. This is
825 // because the runtime performs this translation on load.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000826 //
827 // Fields marked New ABI are part of the GNUstep runtime. We emit them
828 // anyway; the classes will still work with the GNU runtime, they will just
829 // be ignored.
Owen Anderson758428f2009-08-05 23:18:46 +0000830 llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000831 PtrToInt8Ty, // class_pointer
832 PtrToInt8Ty, // super_class
833 PtrToInt8Ty, // name
834 LongTy, // version
835 LongTy, // info
836 LongTy, // instance_size
837 IVars->getType(), // ivars
838 Methods->getType(), // methods
Mike Stump11289f42009-09-09 15:08:12 +0000839 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000840 PtrTy, // dtable
841 PtrTy, // subclass_list
842 PtrTy, // sibling_class
843 PtrTy, // protocols
844 PtrTy, // gc_object_type
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000845 // New ABI:
846 LongTy, // abi_version
847 IvarOffsets->getType(), // ivar_offsets
848 Properties->getType(), // properties
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000849 NULL);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000850 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000851 // Fill in the structure
852 std::vector<llvm::Constant*> Elements;
Owen Andersonade90fd2009-07-29 18:54:39 +0000853 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000854 Elements.push_back(SuperClass);
Chris Lattnerda35bc82008-06-26 04:47:04 +0000855 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000856 Elements.push_back(Zero);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000857 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000858 Elements.push_back(InstanceSize);
859 Elements.push_back(IVars);
860 Elements.push_back(Methods);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000861 Elements.push_back(NULLPtr);
862 Elements.push_back(NULLPtr);
863 Elements.push_back(NULLPtr);
Owen Andersonade90fd2009-07-29 18:54:39 +0000864 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000865 Elements.push_back(NULLPtr);
866 Elements.push_back(Zero);
867 Elements.push_back(IvarOffsets);
868 Elements.push_back(Properties);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000869 // Create an instance of the structure
David Chisnalldf349172010-01-08 00:14:31 +0000870 // This is now an externally visible symbol, so that we can speed up class
871 // messages in the next ABI.
David Chisnalld472c852010-04-28 14:29:56 +0000872 return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_":
873 "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000874}
875
876llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
877 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
878 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
Mike Stump11289f42009-09-09 15:08:12 +0000879 // Get the method structure type.
Owen Anderson758428f2009-08-05 23:18:46 +0000880 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000881 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
882 PtrToInt8Ty,
883 NULL);
884 std::vector<llvm::Constant*> Methods;
885 std::vector<llvm::Constant*> Elements;
886 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
887 Elements.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000888 Elements.push_back(MethodNames[i]);
David Chisnall5778fce2009-08-31 16:41:57 +0000889 Elements.push_back(MethodTypes[i]);
Owen Anderson0e0189d2009-07-27 22:29:56 +0000890 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000891 }
Owen Anderson9793f0e2009-07-29 22:16:19 +0000892 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000893 MethodNames.size());
Owen Anderson47034e12009-07-28 18:33:04 +0000894 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpdd93a192009-07-31 21:31:32 +0000895 Methods);
Owen Anderson758428f2009-08-05 23:18:46 +0000896 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000897 IntTy, ObjCMethodArrayTy, NULL);
898 Methods.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000899 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000900 Methods.push_back(Array);
901 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
902}
Mike Stumpdd93a192009-07-31 21:31:32 +0000903
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000904// Create the protocol list structure used in classes, categories and so on
905llvm::Constant *CGObjCGNU::GenerateProtocolList(
906 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000907 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000908 Protocols.size());
Owen Anderson758428f2009-08-05 23:18:46 +0000909 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000910 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
911 LongTy,//FIXME: Should be size_t
912 ProtocolArrayTy,
913 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000914 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000915 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
916 iter != endIter ; iter++) {
David Chisnallbc8bdea2009-11-20 14:50:59 +0000917 llvm::Constant *protocol = 0;
918 llvm::StringMap<llvm::Constant*>::iterator value =
919 ExistingProtocols.find(*iter);
920 if (value == ExistingProtocols.end()) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000921 protocol = GenerateEmptyProtocol(*iter);
David Chisnallbc8bdea2009-11-20 14:50:59 +0000922 } else {
923 protocol = value->getValue();
924 }
Owen Andersonade90fd2009-07-29 18:54:39 +0000925 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Anderson170229f2009-07-14 23:10:40 +0000926 PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000927 Elements.push_back(Ptr);
928 }
Owen Anderson47034e12009-07-28 18:33:04 +0000929 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000930 Elements);
931 Elements.clear();
932 Elements.push_back(NULLPtr);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000933 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000934 Elements.push_back(ProtocolArray);
935 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
936}
937
Mike Stump11289f42009-09-09 15:08:12 +0000938llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000939 const ObjCProtocolDecl *PD) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000940 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Mike Stump11289f42009-09-09 15:08:12 +0000941 const llvm::Type *T =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000942 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000943 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000944}
945
946llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
947 const std::string &ProtocolName) {
948 llvm::SmallVector<std::string, 0> EmptyStringVector;
949 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
950
951 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000952 llvm::Constant *MethodList =
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000953 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
954 // Protocols are objects containing lists of the methods implemented and
955 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +0000956 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000957 PtrToInt8Ty,
958 ProtocolList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000959 MethodList->getType(),
960 MethodList->getType(),
961 MethodList->getType(),
962 MethodList->getType(),
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000963 NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000964 std::vector<llvm::Constant*> Elements;
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000965 // The isa pointer must be set to a magic number so the runtime knows it's
966 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000967 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
968 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +0000969 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000970 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000971 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
972 Elements.push_back(ProtocolList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000973 Elements.push_back(MethodList);
974 Elements.push_back(MethodList);
975 Elements.push_back(MethodList);
976 Elements.push_back(MethodList);
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000977 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000978}
979
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000980void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
981 ASTContext &Context = CGM.getContext();
Chris Lattner86d7d912008-11-24 03:54:41 +0000982 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000983 llvm::SmallVector<std::string, 16> Protocols;
984 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
985 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +0000986 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000987 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
988 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000989 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
990 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000991 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
992 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +0000993 std::string TypeStr;
994 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000995 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
996 InstanceMethodNames.push_back(
997 MakeConstantString((*iter)->getSelector().getAsString()));
998 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
999 } else {
1000 OptionalInstanceMethodNames.push_back(
1001 MakeConstantString((*iter)->getSelector().getAsString()));
1002 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1003 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001004 }
1005 // Collect information about class methods:
1006 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
1007 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001008 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1009 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001010 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001011 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
1012 iter != endIter ; iter++) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001013 std::string TypeStr;
1014 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001015 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1016 ClassMethodNames.push_back(
1017 MakeConstantString((*iter)->getSelector().getAsString()));
1018 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1019 } else {
1020 OptionalClassMethodNames.push_back(
1021 MakeConstantString((*iter)->getSelector().getAsString()));
1022 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1023 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001024 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001025
1026 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1027 llvm::Constant *InstanceMethodList =
1028 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1029 llvm::Constant *ClassMethodList =
1030 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001031 llvm::Constant *OptionalInstanceMethodList =
1032 GenerateProtocolMethodList(OptionalInstanceMethodNames,
1033 OptionalInstanceMethodTypes);
1034 llvm::Constant *OptionalClassMethodList =
1035 GenerateProtocolMethodList(OptionalClassMethodNames,
1036 OptionalClassMethodTypes);
1037
1038 // Property metadata: name, attributes, isSynthesized, setter name, setter
1039 // types, getter name, getter types.
1040 // The isSynthesized value is always set to 0 in a protocol. It exists to
1041 // simplify the runtime library by allowing it to use the same data
1042 // structures for protocol metadata everywhere.
1043 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1044 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1045 PtrToInt8Ty, NULL);
1046 std::vector<llvm::Constant*> Properties;
1047 std::vector<llvm::Constant*> OptionalProperties;
1048
1049 // Add all of the property methods need adding to the method list and to the
1050 // property metadata list.
1051 for (ObjCContainerDecl::prop_iterator
1052 iter = PD->prop_begin(), endIter = PD->prop_end();
1053 iter != endIter ; iter++) {
1054 std::vector<llvm::Constant*> Fields;
1055 ObjCPropertyDecl *property = (*iter);
1056
1057 Fields.push_back(MakeConstantString(property->getNameAsString()));
1058 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1059 property->getPropertyAttributes()));
1060 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
1061 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1062 std::string TypeStr;
1063 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1064 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1065 InstanceMethodTypes.push_back(TypeEncoding);
1066 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1067 Fields.push_back(TypeEncoding);
1068 } else {
1069 Fields.push_back(NULLPtr);
1070 Fields.push_back(NULLPtr);
1071 }
1072 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1073 std::string TypeStr;
1074 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1075 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1076 InstanceMethodTypes.push_back(TypeEncoding);
1077 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1078 Fields.push_back(TypeEncoding);
1079 } else {
1080 Fields.push_back(NULLPtr);
1081 Fields.push_back(NULLPtr);
1082 }
1083 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1084 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1085 } else {
1086 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1087 }
1088 }
1089 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1090 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1091 llvm::Constant* PropertyListInitFields[] =
1092 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1093
1094 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001095 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001096 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1097 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1098 PropertyListInit, ".objc_property_list");
1099
1100 llvm::Constant *OptionalPropertyArray =
1101 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1102 OptionalProperties.size()) , OptionalProperties);
1103 llvm::Constant* OptionalPropertyListInitFields[] = {
1104 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1105 OptionalPropertyArray };
1106
1107 llvm::Constant *OptionalPropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001108 llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001109 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1110 OptionalPropertyListInit->getType(), false,
1111 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1112 ".objc_property_list");
1113
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001114 // Protocols are objects containing lists of the methods implemented and
1115 // protocols adopted.
Owen Anderson758428f2009-08-05 23:18:46 +00001116 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001117 PtrToInt8Ty,
1118 ProtocolList->getType(),
1119 InstanceMethodList->getType(),
1120 ClassMethodList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001121 OptionalInstanceMethodList->getType(),
1122 OptionalClassMethodList->getType(),
1123 PropertyList->getType(),
1124 OptionalPropertyList->getType(),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001125 NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001126 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001127 // The isa pointer must be set to a magic number so the runtime knows it's
1128 // the correct layout.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001129 int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ?
1130 NonFragileProtocolVersion : ProtocolVersion;
Owen Andersonade90fd2009-07-29 18:54:39 +00001131 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001132 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001133 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1134 Elements.push_back(ProtocolList);
1135 Elements.push_back(InstanceMethodList);
1136 Elements.push_back(ClassMethodList);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001137 Elements.push_back(OptionalInstanceMethodList);
1138 Elements.push_back(OptionalClassMethodList);
1139 Elements.push_back(PropertyList);
1140 Elements.push_back(OptionalPropertyList);
Mike Stump11289f42009-09-09 15:08:12 +00001141 ExistingProtocols[ProtocolName] =
Owen Andersonade90fd2009-07-29 18:54:39 +00001142 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001143 ".objc_protocol"), IdTy);
1144}
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001145void CGObjCGNU::GenerateProtocolHolderCategory(void) {
1146 // Collect information about instance methods
1147 llvm::SmallVector<Selector, 1> MethodSels;
1148 llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
1149
1150 std::vector<llvm::Constant*> Elements;
1151 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1152 const std::string CategoryName = "AnotherHack";
1153 Elements.push_back(MakeConstantString(CategoryName));
1154 Elements.push_back(MakeConstantString(ClassName));
1155 // Instance method list
1156 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1157 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1158 // Class method list
1159 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1160 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1161 // Protocol list
1162 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1163 ExistingProtocols.size());
1164 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
1165 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1166 LongTy,//FIXME: Should be size_t
1167 ProtocolArrayTy,
1168 NULL);
1169 std::vector<llvm::Constant*> ProtocolElements;
1170 for (llvm::StringMapIterator<llvm::Constant*> iter =
1171 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1172 iter != endIter ; iter++) {
1173 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1174 PtrTy);
1175 ProtocolElements.push_back(Ptr);
1176 }
1177 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1178 ProtocolElements);
1179 ProtocolElements.clear();
1180 ProtocolElements.push_back(NULLPtr);
1181 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1182 ExistingProtocols.size()));
1183 ProtocolElements.push_back(ProtocolArray);
1184 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1185 ProtocolElements, ".objc_protocol_list"), PtrTy));
1186 Categories.push_back(llvm::ConstantExpr::getBitCast(
1187 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
1188 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1189}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001190
Daniel Dunbar92992502008-08-15 22:20:32 +00001191void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001192 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1193 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar92992502008-08-15 22:20:32 +00001194 // Collect information about instance methods
1195 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1196 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001197 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001198 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001199 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001200 InstanceMethodSels.push_back((*iter)->getSelector());
1201 std::string TypeStr;
1202 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001203 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001204 }
1205
1206 // Collect information about class methods
1207 llvm::SmallVector<Selector, 16> ClassMethodSels;
1208 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001209 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001210 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001211 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001212 ClassMethodSels.push_back((*iter)->getSelector());
1213 std::string TypeStr;
1214 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001215 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001216 }
1217
1218 // Collect the names of referenced protocols
1219 llvm::SmallVector<std::string, 16> Protocols;
David Chisnall2bfc50b2010-03-13 22:20:45 +00001220 const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
1221 const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
Daniel Dunbar92992502008-08-15 22:20:32 +00001222 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1223 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001224 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001225
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001226 std::vector<llvm::Constant*> Elements;
1227 Elements.push_back(MakeConstantString(CategoryName));
1228 Elements.push_back(MakeConstantString(ClassName));
Mike Stump11289f42009-09-09 15:08:12 +00001229 // Instance method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001230 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001231 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001232 false), PtrTy));
1233 // Class method list
Owen Andersonade90fd2009-07-29 18:54:39 +00001234 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnerbf231a62008-06-26 05:08:00 +00001235 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001236 PtrTy));
1237 // Protocol list
Owen Andersonade90fd2009-07-29 18:54:39 +00001238 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001239 GenerateProtocolList(Protocols), PtrTy));
Owen Andersonade90fd2009-07-29 18:54:39 +00001240 Categories.push_back(llvm::ConstantExpr::getBitCast(
Mike Stump11289f42009-09-09 15:08:12 +00001241 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson758428f2009-08-05 23:18:46 +00001242 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001243}
Daniel Dunbar92992502008-08-15 22:20:32 +00001244
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001245llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1246 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1247 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1248 ASTContext &Context = CGM.getContext();
1249 //
1250 // Property metadata: name, attributes, isSynthesized, setter name, setter
1251 // types, getter name, getter types.
1252 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext,
1253 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1254 PtrToInt8Ty, NULL);
1255 std::vector<llvm::Constant*> Properties;
1256
1257
1258 // Add all of the property methods need adding to the method list and to the
1259 // property metadata list.
1260 for (ObjCImplDecl::propimpl_iterator
1261 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1262 iter != endIter ; iter++) {
1263 std::vector<llvm::Constant*> Fields;
1264 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
David Chisnall36c63202010-02-26 01:11:38 +00001265 ObjCPropertyImplDecl *propertyImpl = *iter;
1266 bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
1267 ObjCPropertyImplDecl::Synthesize);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001268
1269 Fields.push_back(MakeConstantString(property->getNameAsString()));
1270 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1271 property->getPropertyAttributes()));
David Chisnall36c63202010-02-26 01:11:38 +00001272 Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001273 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001274 std::string TypeStr;
1275 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1276 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall36c63202010-02-26 01:11:38 +00001277 if (isSynthesized) {
1278 InstanceMethodTypes.push_back(TypeEncoding);
1279 InstanceMethodSels.push_back(getter->getSelector());
1280 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001281 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1282 Fields.push_back(TypeEncoding);
1283 } else {
1284 Fields.push_back(NULLPtr);
1285 Fields.push_back(NULLPtr);
1286 }
1287 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001288 std::string TypeStr;
1289 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1290 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall36c63202010-02-26 01:11:38 +00001291 if (isSynthesized) {
1292 InstanceMethodTypes.push_back(TypeEncoding);
1293 InstanceMethodSels.push_back(setter->getSelector());
1294 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001295 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1296 Fields.push_back(TypeEncoding);
1297 } else {
1298 Fields.push_back(NULLPtr);
1299 Fields.push_back(NULLPtr);
1300 }
1301 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1302 }
1303 llvm::ArrayType *PropertyArrayTy =
1304 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1305 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1306 Properties);
1307 llvm::Constant* PropertyListInitFields[] =
1308 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1309
1310 llvm::Constant *PropertyListInit =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001311 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001312 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1313 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1314 ".objc_property_list");
1315}
1316
Daniel Dunbar92992502008-08-15 22:20:32 +00001317void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1318 ASTContext &Context = CGM.getContext();
1319
1320 // Get the superclass name.
Mike Stump11289f42009-09-09 15:08:12 +00001321 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar92992502008-08-15 22:20:32 +00001322 OID->getClassInterface()->getSuperClass();
Chris Lattner86d7d912008-11-24 03:54:41 +00001323 std::string SuperClassName;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001324 if (SuperClassDecl) {
Chris Lattner86d7d912008-11-24 03:54:41 +00001325 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001326 EmitClassRef(SuperClassName);
1327 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001328
1329 // Get the class name
Chris Lattner87bc3872009-04-01 02:00:48 +00001330 ObjCInterfaceDecl *ClassDecl =
1331 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner86d7d912008-11-24 03:54:41 +00001332 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00001333 // Emit the symbol that is used to generate linker errors if this class is
1334 // referenced in other modules but not declared.
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001335 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump11289f42009-09-09 15:08:12 +00001336 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001337 TheModule.getGlobalVariable(classSymbolName)) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001338 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001339 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00001340 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001341 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Andersonc10c8d32009-07-08 19:05:04 +00001342 classSymbolName);
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00001343 }
Mike Stump11289f42009-09-09 15:08:12 +00001344
Daniel Dunbar12119b92009-05-03 10:46:44 +00001345 // Get the size of instances.
1346 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar92992502008-08-15 22:20:32 +00001347
1348 // Collect information about instance variables.
1349 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
1350 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
1351 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump11289f42009-09-09 15:08:12 +00001352
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001353 std::vector<llvm::Constant*> IvarOffsetValues;
1354
Mike Stump11289f42009-09-09 15:08:12 +00001355 int superInstanceSize = !SuperClassDecl ? 0 :
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001356 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
1357 // For non-fragile ivars, set the instance size to 0 - {the size of just this
1358 // class}. The runtime will then set this to the correct value on load.
1359 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1360 instanceSize = 0 - (instanceSize - superInstanceSize);
1361 }
David Chisnall18cf7372010-04-19 00:45:34 +00001362
1363 // Collect declared and synthesized ivars.
1364 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
1365 CGM.getContext().ShallowCollectObjCIvars(ClassDecl, OIvars);
1366
1367 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
1368 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar92992502008-08-15 22:20:32 +00001369 // Store the name
David Chisnall18cf7372010-04-19 00:45:34 +00001370 IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001371 // Get the type encoding for this ivar
1372 std::string TypeStr;
David Chisnall18cf7372010-04-19 00:45:34 +00001373 Context.getObjCEncodingForType(IVD->getType(), TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001374 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001375 // Get the offset
David Chisnall44ec5552010-04-19 01:37:25 +00001376 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
David Chisnallcb1b7bf2009-11-17 19:32:15 +00001377 uint64_t Offset = BaseOffset;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001378 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001379 Offset = BaseOffset - superInstanceSize;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001380 }
Daniel Dunbar92992502008-08-15 22:20:32 +00001381 IvarOffsets.push_back(
Owen Anderson41a75022009-08-13 21:57:51 +00001382 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001383 IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1384 false, llvm::GlobalValue::ExternalLinkage,
1385 llvm::ConstantInt::get(IntTy, BaseOffset),
1386 "__objc_ivar_offset_value_" + ClassName +"." +
David Chisnall18cf7372010-04-19 00:45:34 +00001387 IVD->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00001388 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001389 llvm::Constant *IvarOffsetArrayInit =
1390 llvm::ConstantArray::get(llvm::ArrayType::get(PtrToIntTy,
1391 IvarOffsetValues.size()), IvarOffsetValues);
1392 llvm::GlobalVariable *IvarOffsetArray = new llvm::GlobalVariable(TheModule,
1393 IvarOffsetArrayInit->getType(), false,
1394 llvm::GlobalValue::InternalLinkage, IvarOffsetArrayInit,
1395 ".ivar.offsets");
Daniel Dunbar92992502008-08-15 22:20:32 +00001396
1397 // Collect information about instance methods
1398 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1399 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump11289f42009-09-09 15:08:12 +00001400 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001401 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001402 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001403 InstanceMethodSels.push_back((*iter)->getSelector());
1404 std::string TypeStr;
1405 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001406 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001407 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001408
1409 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1410 InstanceMethodTypes);
1411
Daniel Dunbar92992502008-08-15 22:20:32 +00001412
1413 // Collect information about class methods
1414 llvm::SmallVector<Selector, 16> ClassMethodSels;
1415 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001416 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001417 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001418 iter != endIter ; iter++) {
Daniel Dunbar92992502008-08-15 22:20:32 +00001419 ClassMethodSels.push_back((*iter)->getSelector());
1420 std::string TypeStr;
1421 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00001422 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00001423 }
1424 // Collect the names of referenced protocols
1425 llvm::SmallVector<std::string, 16> Protocols;
1426 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1427 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1428 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00001429 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00001430
1431
1432
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001433 // Get the superclass pointer.
1434 llvm::Constant *SuperClass;
Chris Lattner86d7d912008-11-24 03:54:41 +00001435 if (!SuperClassName.empty()) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001436 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
1437 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +00001438 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001439 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001440 // Empty vector used to construct empty method lists
1441 llvm::SmallVector<llvm::Constant*, 1> empty;
1442 // Generate the method and instance variable lists
1443 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001444 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001445 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00001446 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001447 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
1448 IvarOffsets);
Mike Stump11289f42009-09-09 15:08:12 +00001449 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall5778fce2009-08-31 16:41:57 +00001450 // we emit a symbol containing the offset for each ivar in the class. This
1451 // allows code compiled for the non-Fragile ABI to inherit from code compiled
1452 // for the legacy ABI, without causing problems. The converse is also
1453 // possible, but causes all ivar accesses to be fragile.
1454 int i = 0;
1455 // Offset pointer for getting at the correct field in the ivar list when
1456 // setting up the alias. These are: The base address for the global, the
1457 // ivar array (second field), the ivar in this list (set for each ivar), and
1458 // the offset (third field in ivar structure)
1459 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
1460 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump11289f42009-09-09 15:08:12 +00001461 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall5778fce2009-08-31 16:41:57 +00001462 llvm::ConstantInt::get(IndexTy, 2) };
1463
1464 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
1465 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
1466 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
1467 +(*iter)->getNameAsString();
1468 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++);
1469 // Get the correct ivar field
1470 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1471 IvarList, offsetPointerIndexes, 4);
1472 // Get the existing alias, if one exists.
1473 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1474 if (offset) {
1475 offset->setInitializer(offsetValue);
1476 // If this is the real definition, change its linkage type so that
1477 // different modules will use this one, rather than their private
1478 // copy.
1479 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1480 } else {
1481 // Add a new alias if there isn't one already.
1482 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1483 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1484 }
1485 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001486 //Generate metaclass for class methods
1487 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnallb3b44ce2009-11-16 19:05:54 +00001488 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
David Chisnalld472c852010-04-28 14:29:56 +00001489 empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001490
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001491 // Generate the class structure
Chris Lattner86d7d912008-11-24 03:54:41 +00001492 llvm::Constant *ClassStruct =
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001493 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner86d7d912008-11-24 03:54:41 +00001494 ClassName.c_str(), 0,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001495 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001496 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1497 Properties);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001498
1499 // Resolve the class aliases, if they exist.
1500 if (ClassPtrAlias) {
1501 ClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001502 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001503 ClassPtrAlias = 0;
1504 }
1505 if (MetaClassPtrAlias) {
1506 MetaClassPtrAlias->setAliasee(
Owen Andersonade90fd2009-07-29 18:54:39 +00001507 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001508 MetaClassPtrAlias = 0;
1509 }
1510
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001511 // Add class structure to list to be added to the symtab later
Owen Andersonade90fd2009-07-29 18:54:39 +00001512 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001513 Classes.push_back(ClassStruct);
1514}
1515
Fariborz Jahanian248c7192009-06-23 21:47:46 +00001516
Mike Stump11289f42009-09-09 15:08:12 +00001517llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001518 // Only emit an ObjC load function if no Objective-C stuff has been called
1519 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1520 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov3b6dd582008-06-01 15:14:46 +00001521 UntypedSelectors.empty())
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001522 return NULL;
Eli Friedman412c6682008-06-01 16:00:02 +00001523
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001524 // Add all referenced protocols to a category.
1525 GenerateProtocolHolderCategory();
1526
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001527 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1528 SelectorTy->getElementType());
1529 const llvm::Type *SelStructPtrTy = SelectorTy;
1530 bool isSelOpaque = false;
1531 if (SelStructTy == 0) {
Owen Anderson758428f2009-08-05 23:18:46 +00001532 SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
1533 PtrToInt8Ty, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001534 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001535 isSelOpaque = true;
1536 }
1537
Eli Friedman412c6682008-06-01 16:00:02 +00001538 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001539 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman412c6682008-06-01 16:00:02 +00001540 TheModule.addTypeName(".objc_id", IdTy);
1541 TheModule.addTypeName(".objc_imp", IMPTy);
1542
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001543 std::vector<llvm::Constant*> Elements;
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001544 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001545 // Generate statics list:
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001546 if (ConstantStrings.size()) {
Owen Anderson9793f0e2009-07-29 22:16:19 +00001547 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001548 ConstantStrings.size() + 1);
1549 ConstantStrings.push_back(NULLPtr);
David Chisnall5778fce2009-08-31 16:41:57 +00001550
Daniel Dunbar75fa84e2009-11-29 02:38:47 +00001551 llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
1552 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall5778fce2009-08-31 16:41:57 +00001553 Elements.push_back(MakeConstantString(StringClass,
1554 ".objc_static_class_name"));
Owen Anderson47034e12009-07-28 18:33:04 +00001555 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001556 ConstantStrings));
Mike Stump11289f42009-09-09 15:08:12 +00001557 llvm::StructType *StaticsListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00001558 llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Anderson170229f2009-07-14 23:10:40 +00001559 llvm::Type *StaticsListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001560 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001561 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump11289f42009-09-09 15:08:12 +00001562 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001563 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001564 Elements.clear();
1565 Elements.push_back(Statics);
Owen Anderson0b75f232009-07-31 20:28:54 +00001566 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001567 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Andersonade90fd2009-07-29 18:54:39 +00001568 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001569 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001570 // Array of classes, categories, and constant objects
Owen Anderson9793f0e2009-07-29 22:16:19 +00001571 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001572 Classes.size() + Categories.size() + 2);
Mike Stump11289f42009-09-09 15:08:12 +00001573 llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00001574 LongTy, SelStructPtrTy,
Owen Anderson41a75022009-08-13 21:57:51 +00001575 llvm::Type::getInt16Ty(VMContext),
1576 llvm::Type::getInt16Ty(VMContext),
Chris Lattner63dd3372008-06-26 04:10:42 +00001577 ClassListTy, NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001578
1579 Elements.clear();
1580 // Pointer to an array of selectors used in this module.
1581 std::vector<llvm::Constant*> Selectors;
1582 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1583 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1584 iter != iterEnd ; ++iter) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001585 Elements.push_back(ExportUniqueString(iter->first.first, ".objc_sel_name"));
David Chisnall29979822009-09-14 19:04:10 +00001586 Elements.push_back(MakeConstantString(iter->first.second,
Chris Lattner63dd3372008-06-26 04:10:42 +00001587 ".objc_sel_types"));
Owen Anderson0e0189d2009-07-27 22:29:56 +00001588 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001589 Elements.clear();
1590 }
1591 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1592 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner63dd3372008-06-26 04:10:42 +00001593 iter != iterEnd; ++iter) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001594 Elements.push_back(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001595 ExportUniqueString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001596 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001597 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001598 Elements.clear();
1599 }
1600 Elements.push_back(NULLPtr);
1601 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001602 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001603 Elements.clear();
1604 // Number of static selectors
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001605 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001606 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson9793f0e2009-07-29 22:16:19 +00001607 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001608 ".objc_selector_list");
Mike Stump11289f42009-09-09 15:08:12 +00001609 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001610 SelStructPtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001611
1612 // Now that all of the static selectors exist, create pointers to them.
1613 int index = 0;
1614 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1615 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1616 iter != iterEnd; ++iter) {
1617 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001618 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Daniel Dunbar45858d22010-02-03 20:11:42 +00001619 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1620 true, llvm::GlobalValue::InternalLinkage,
1621 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1622 ".objc_sel_ptr");
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001623 // If selectors are defined as an opaque type, cast the pointer to this
1624 // type.
1625 if (isSelOpaque) {
Daniel Dunbar45858d22010-02-03 20:11:42 +00001626 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
1627 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001628 }
Daniel Dunbar45858d22010-02-03 20:11:42 +00001629 (*iter).second->setAliasee(SelPtr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001630 }
1631 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1632 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1633 iter != iterEnd; iter++) {
1634 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson41a75022009-08-13 21:57:51 +00001635 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Daniel Dunbar45858d22010-02-03 20:11:42 +00001636 llvm::Constant *SelPtr = new llvm::GlobalVariable
1637 (TheModule, SelStructPtrTy,
1638 true, llvm::GlobalValue::InternalLinkage,
1639 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1640 ".objc_sel_ptr");
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001641 // If selectors are defined as an opaque type, cast the pointer to this
1642 // type.
1643 if (isSelOpaque) {
Daniel Dunbar45858d22010-02-03 20:11:42 +00001644 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
1645 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattner8d3f4a42009-01-27 05:06:01 +00001646 }
Daniel Dunbar45858d22010-02-03 20:11:42 +00001647 (*iter).second->setAliasee(SelPtr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001648 }
1649 // Number of classes defined.
Mike Stump11289f42009-09-09 15:08:12 +00001650 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001651 Classes.size()));
1652 // Number of categories defined
Mike Stump11289f42009-09-09 15:08:12 +00001653 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001654 Categories.size()));
1655 // Create an array of classes, then categories, then static object instances
1656 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1657 // NULL-terminated list of static object instances (mainly constant strings)
1658 Classes.push_back(Statics);
1659 Classes.push_back(NULLPtr);
Owen Anderson47034e12009-07-28 18:33:04 +00001660 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001661 Elements.push_back(ClassList);
Mike Stump11289f42009-09-09 15:08:12 +00001662 // Construct the symbol table
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001663 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1664
1665 // The symbol table is contained in a module which has some version-checking
1666 // constants
Owen Anderson758428f2009-08-05 23:18:46 +00001667 llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001668 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001669 Elements.clear();
1670 // Runtime version used for compatibility checking.
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001671 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Mike Stump11289f42009-09-09 15:08:12 +00001672 Elements.push_back(llvm::ConstantInt::get(LongTy,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001673 NonFragileRuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001674 } else {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001675 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001676 }
Fariborz Jahanianc2d56182009-04-01 19:49:42 +00001677 // sizeof(ModuleTy)
Benjamin Kramerf3a499a2010-02-09 19:31:24 +00001678 llvm::TargetData td(&TheModule);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001679 Elements.push_back(llvm::ConstantInt::get(LongTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001680 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001681 //FIXME: Should be the path to the file where this module was declared
1682 Elements.push_back(NULLPtr);
1683 Elements.push_back(SymTab);
1684 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1685
1686 // Create the load function calling the runtime entry point with the module
1687 // structure
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001688 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson41a75022009-08-13 21:57:51 +00001689 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001690 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1691 &TheModule);
Owen Anderson41a75022009-08-13 21:57:51 +00001692 llvm::BasicBlock *EntryBB =
1693 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Anderson170229f2009-07-14 23:10:40 +00001694 CGBuilderTy Builder(VMContext);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001695 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian3b636c12009-03-30 18:02:14 +00001696
1697 std::vector<const llvm::Type*> Params(1,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001698 llvm::PointerType::getUnqual(ModuleTy));
1699 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +00001700 llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001701 Builder.CreateCall(Register, Module);
1702 Builder.CreateRetVoid();
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00001703
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001704 return LoadFunction;
1705}
Daniel Dunbar92992502008-08-15 22:20:32 +00001706
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00001707llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump11289f42009-09-09 15:08:12 +00001708 const ObjCContainerDecl *CD) {
1709 const ObjCCategoryImplDecl *OCD =
Steve Naroff11b387f2009-01-08 19:41:02 +00001710 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattnere4b95692008-11-24 03:33:13 +00001711 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
David Chisnall12ae54d2010-03-20 19:53:29 +00001712 std::string ClassName = CD->getName();
Chris Lattnere4b95692008-11-24 03:33:13 +00001713 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorffca3a22009-01-09 17:18:27 +00001714 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar92992502008-08-15 22:20:32 +00001715
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001716 CodeGenTypes &Types = CGM.getTypes();
Mike Stump11289f42009-09-09 15:08:12 +00001717 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001718 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001719 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1720 MethodName, isClassMethod);
1721
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001722 llvm::Function *Method
Mike Stump11289f42009-09-09 15:08:12 +00001723 = llvm::Function::Create(MethodTy,
1724 llvm::GlobalValue::InternalLinkage,
1725 FunctionName,
1726 &TheModule);
Chris Lattner4bd55962008-03-30 23:03:07 +00001727 return Method;
1728}
1729
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001730llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Mike Stump11289f42009-09-09 15:08:12 +00001731 std::vector<const llvm::Type*> Params;
1732 const llvm::Type *BoolTy =
1733 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1734 Params.push_back(IdTy);
1735 Params.push_back(SelectorTy);
David Chisnall18cf7372010-04-19 00:45:34 +00001736 Params.push_back(IntTy);
Mike Stump11289f42009-09-09 15:08:12 +00001737 Params.push_back(BoolTy);
David Chisnall18cf7372010-04-19 00:45:34 +00001738 // void objc_getProperty (id, SEL, int, bool)
Mike Stump11289f42009-09-09 15:08:12 +00001739 const llvm::FunctionType *FTy =
1740 llvm::FunctionType::get(IdTy, Params, false);
1741 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1742 "objc_getProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001743}
1744
1745llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Mike Stump11289f42009-09-09 15:08:12 +00001746 std::vector<const llvm::Type*> Params;
1747 const llvm::Type *BoolTy =
1748 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1749 Params.push_back(IdTy);
1750 Params.push_back(SelectorTy);
David Chisnall18cf7372010-04-19 00:45:34 +00001751 Params.push_back(IntTy);
Mike Stump11289f42009-09-09 15:08:12 +00001752 Params.push_back(IdTy);
1753 Params.push_back(BoolTy);
1754 Params.push_back(BoolTy);
David Chisnall18cf7372010-04-19 00:45:34 +00001755 // void objc_setProperty (id, SEL, int, id, bool, bool)
Mike Stump11289f42009-09-09 15:08:12 +00001756 const llvm::FunctionType *FTy =
1757 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
1758 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1759 "objc_setProperty"));
Daniel Dunbara91c3e02008-09-24 03:38:44 +00001760}
1761
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001762// FIXME. Implement this.
1763llvm::Function *CGObjCGNU::GetCopyStructFunction() {
1764 return 0;
1765}
1766
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001767llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
1768 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1769 ASTContext &Ctx = CGM.getContext();
1770 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +00001771 llvm::SmallVector<CanQualType,1> Params;
David Chisnall9f57c292009-08-17 16:35:33 +00001772 Params.push_back(ASTIdTy);
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001773 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +00001774 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001775 FunctionType::ExtInfo()), false);
Daniel Dunbarc46a0792009-07-24 07:40:24 +00001776 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
Anders Carlsson3f35a262008-08-31 04:05:03 +00001777}
1778
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00001779void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1780 const Stmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001781 // Pointer to the personality function
1782 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +00001783 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +00001784 true),
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001785 "__gnu_objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +00001786 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001787 std::vector<const llvm::Type*> Params;
1788 Params.push_back(PtrTy);
1789 llvm::Value *RethrowFn =
Owen Anderson41a75022009-08-13 21:57:51 +00001790 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
David Chisnall9a2073c2010-01-06 18:02:59 +00001791 Params, false), "_Unwind_Resume");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001792
1793 bool isTry = isa<ObjCAtTryStmt>(S);
1794 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1795 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1796 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattner9fea9442009-05-11 18:16:28 +00001797 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001798 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1799 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1800 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1801
David Chisnall3a509cd2009-12-24 02:26:34 +00001802 // @synchronized()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001803 if (!isTry) {
Chris Lattner9fea9442009-05-11 18:16:28 +00001804 std::vector<const llvm::Type*> Args(1, IdTy);
1805 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001806 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001807 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
Mike Stump11289f42009-09-09 15:08:12 +00001808 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001809 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1810 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1811 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001812 }
1813
Chris Lattner9fea9442009-05-11 18:16:28 +00001814
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001815 // Push an EH context entry, used for handling rethrows and jumps
1816 // through finally.
1817 CGF.PushCleanupBlock(FinallyBlock);
1818
1819 // Emit the statements in the @try {} block
1820 CGF.setInvokeDest(TryHandler);
1821
1822 CGF.EmitBlock(TryBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001823 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001824 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1825
1826 // Jump to @finally if there is no exception
1827 CGF.EmitBranchThroughCleanup(FinallyEnd);
1828
1829 // Emit the handlers
1830 CGF.EmitBlock(TryHandler);
1831
Chris Lattner96afab52009-05-08 17:36:08 +00001832 // Get the correct versions of the exception handling intrinsics
Mike Stump11289f42009-09-09 15:08:12 +00001833 llvm::Value *llvm_eh_exception =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001834 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Duncan Sandscef56992009-10-14 16:13:30 +00001835 llvm::Value *llvm_eh_selector =
1836 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
1837 llvm::Value *llvm_eh_typeid_for =
1838 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001839
1840 // Exception object
1841 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1842 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1843
1844 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
Douglas Gregor46a572b2010-04-26 16:46:50 +00001845 llvm::SmallVector<std::pair<const VarDecl*, const Stmt*>, 8> Handlers;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001846
1847 ESelArgs.push_back(Exc);
1848 ESelArgs.push_back(Personality);
1849
1850 bool HasCatchAll = false;
1851 // Only @try blocks are allowed @catch blocks, but both can have @finally
1852 if (isTry) {
Douglas Gregor96c79492010-04-23 22:50:49 +00001853 if (cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
1854 const ObjCAtTryStmt &AtTry = cast<ObjCAtTryStmt>(S);
Chris Lattner9fea9442009-05-11 18:16:28 +00001855 CGF.setInvokeDest(CatchInCatch);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001856
Douglas Gregor96c79492010-04-23 22:50:49 +00001857 for (unsigned I = 0, N = AtTry.getNumCatchStmts(); I != N; ++I) {
1858 const ObjCAtCatchStmt *CatchStmt = AtTry.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00001859 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Mike Stumpdd93a192009-07-31 21:31:32 +00001860 Handlers.push_back(std::make_pair(CatchDecl,
1861 CatchStmt->getCatchBody()));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001862
1863 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff7cae42b2009-07-10 23:34:53 +00001864 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001865 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1866 // Use i8* null here to signal this is a catch all, not a cleanup.
1867 ESelArgs.push_back(NULLPtr);
1868 HasCatchAll = true;
1869 // No further catches after this one will ever by reached
1870 break;
Mike Stump11289f42009-09-09 15:08:12 +00001871 }
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001872
1873 // All other types should be Objective-C interface pointer types.
Mike Stump11289f42009-09-09 15:08:12 +00001874 const ObjCObjectPointerType *OPT =
John McCall9dd450b2009-09-21 23:43:11 +00001875 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001876 assert(OPT && "Invalid @catch type.");
Mike Stump11289f42009-09-09 15:08:12 +00001877 const ObjCInterfaceType *IT =
John McCall9dd450b2009-09-21 23:43:11 +00001878 OPT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001879 assert(IT && "Invalid @catch type.");
Chris Lattner96afab52009-05-08 17:36:08 +00001880 llvm::Value *EHType =
1881 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001882 ESelArgs.push_back(EHType);
1883 }
1884 }
1885 }
1886
1887 // We use a cleanup unless there was already a catch all.
1888 if (!HasCatchAll) {
Owen Anderson41a75022009-08-13 21:57:51 +00001889 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001890 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1891 }
1892
1893 // Find which handler was matched.
Chris Lattner96afab52009-05-08 17:36:08 +00001894 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001895 ESelArgs.begin(), ESelArgs.end(), "selector");
1896
1897 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Douglas Gregor46a572b2010-04-26 16:46:50 +00001898 const VarDecl *CatchParam = Handlers[i].first;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001899 const Stmt *CatchBody = Handlers[i].second;
1900
1901 llvm::BasicBlock *Next = 0;
1902
1903 // The last handler always matches.
1904 if (i + 1 != e) {
1905 assert(CatchParam && "Only last handler can be a catch all.");
1906
1907 // Test whether this block matches the type for the selector and branch
1908 // to Match if it does, or to the next BB if it doesn't.
1909 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1910 Next = CGF.createBasicBlock("catch.next");
Chris Lattner96afab52009-05-08 17:36:08 +00001911 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001912 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1913 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1914 Next);
1915
1916 CGF.EmitBlock(Match);
1917 }
Mike Stump11289f42009-09-09 15:08:12 +00001918
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001919 if (CatchBody) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001920 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1921 CGF.ConvertType(CatchParam->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001922
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001923 // Bind the catch parameter if it exists.
1924 if (CatchParam) {
1925 // CatchParam is a ParmVarDecl because of the grammar
1926 // construction used to handle this, but for codegen purposes
1927 // we treat this as a local decl.
1928 CGF.EmitLocalBlockVarDecl(*CatchParam);
1929 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1930 }
1931
1932 CGF.ObjCEHValueStack.push_back(ExcObject);
1933 CGF.EmitStmt(CatchBody);
1934 CGF.ObjCEHValueStack.pop_back();
1935
1936 CGF.EmitBranchThroughCleanup(FinallyEnd);
1937
1938 if (Next)
1939 CGF.EmitBlock(Next);
1940 } else {
1941 assert(!Next && "catchup should be last handler.");
1942
1943 CGF.Builder.CreateStore(Exc, RethrowPtr);
1944 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1945 }
1946 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001947 // The @finally block is a secondary landing pad for any exceptions thrown in
1948 // @catch() blocks
1949 CGF.EmitBlock(CatchInCatch);
1950 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1951 ESelArgs.clear();
1952 ESelArgs.push_back(Exc);
1953 ESelArgs.push_back(Personality);
David Chisnall3a509cd2009-12-24 02:26:34 +00001954 // If there is a @catch or @finally clause in outside of this one then we
1955 // need to make sure that we catch and rethrow it.
1956 if (PrevLandingPad) {
1957 ESelArgs.push_back(NULLPtr);
1958 } else {
1959 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
1960 }
Chris Lattner9fea9442009-05-11 18:16:28 +00001961 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1962 "selector");
1963 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1964 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1965 CGF.Builder.CreateStore(Exc, RethrowPtr);
1966 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1967
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001968 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1969
1970 CGF.setInvokeDest(PrevLandingPad);
1971
1972 CGF.EmitBlock(FinallyBlock);
1973
Chris Lattner9fea9442009-05-11 18:16:28 +00001974
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001975 if (isTry) {
Mike Stump11289f42009-09-09 15:08:12 +00001976 if (const ObjCAtFinallyStmt* FinallyStmt =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001977 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1978 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1979 } else {
Chris Lattner9fea9442009-05-11 18:16:28 +00001980 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001981 // @synchronized.
Chris Lattner9fea9442009-05-11 18:16:28 +00001982 std::vector<const llvm::Type*> Args(1, IdTy);
1983 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00001984 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner9fea9442009-05-11 18:16:28 +00001985 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Mike Stump11289f42009-09-09 15:08:12 +00001986 llvm::Value *SyncArg =
Chris Lattner9fea9442009-05-11 18:16:28 +00001987 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1988 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1989 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00001990 }
1991
1992 if (Info.SwitchBlock)
1993 CGF.EmitBlock(Info.SwitchBlock);
1994 if (Info.EndBlock)
1995 CGF.EmitBlock(Info.EndBlock);
1996
1997 // Branch around the rethrow code.
1998 CGF.EmitBranch(FinallyEnd);
1999
2000 CGF.EmitBlock(FinallyRethrow);
David Chisnall3a509cd2009-12-24 02:26:34 +00002001
2002 llvm::Value *ExceptionObject = CGF.Builder.CreateLoad(RethrowPtr);
2003 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
2004 if (!UnwindBB) {
2005 CGF.Builder.CreateCall(RethrowFn, ExceptionObject);
2006 // Exception always thrown, next instruction is never reached.
2007 CGF.Builder.CreateUnreachable();
2008 } else {
2009 // If there is a @catch block outside this scope, we invoke instead of
2010 // calling because we may return to this function. This is very slow, but
2011 // some people still do it. It would be nice to add an optimised path for
2012 // this.
2013 CGF.Builder.CreateInvoke(RethrowFn, UnwindBB, UnwindBB, &ExceptionObject,
2014 &ExceptionObject+1);
2015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002017 CGF.EmitBlock(FinallyEnd);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002018}
2019
2020void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002021 const ObjCAtThrowStmt &S) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002022 llvm::Value *ExceptionAsObject;
2023
2024 std::vector<const llvm::Type*> Args(1, IdTy);
2025 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +00002026 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Mike Stump11289f42009-09-09 15:08:12 +00002027 llvm::Value *ThrowFn =
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002028 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Mike Stump11289f42009-09-09 15:08:12 +00002029
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002030 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2031 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian078cd522009-05-17 16:49:27 +00002032 ExceptionAsObject = Exception;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002033 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002034 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002035 "Unexpected rethrow outside @catch block.");
2036 ExceptionAsObject = CGF.ObjCEHValueStack.back();
2037 }
Fariborz Jahanian078cd522009-05-17 16:49:27 +00002038 ExceptionAsObject =
2039 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +00002040
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002041 // Note: This may have to be an invoke, if we want to support constructs like:
2042 // @try {
2043 // @throw(obj);
2044 // }
2045 // @catch(id) ...
2046 //
2047 // This is effectively turning @throw into an incredibly-expensive goto, but
2048 // it may happen as a result of inlining followed by missed optimizations, or
2049 // as a result of stupidity.
2050 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
2051 if (!UnwindBB) {
2052 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
2053 CGF.Builder.CreateUnreachable();
2054 } else {
2055 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
2056 &ExceptionAsObject+1);
2057 }
2058 // Clear the insertion point to indicate we are in unreachable code.
2059 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002060}
2061
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002062llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002063 llvm::Value *AddrWeakObj) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002064 CGBuilderTy B = CGF.Builder;
2065 AddrWeakObj = EnforceType(B, AddrWeakObj, IdTy);
2066 return B.CreateCall(WeakReadFn, AddrWeakObj);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00002067}
2068
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002069void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002070 llvm::Value *src, llvm::Value *dst) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002071 CGBuilderTy B = CGF.Builder;
2072 src = EnforceType(B, src, IdTy);
2073 dst = EnforceType(B, dst, PtrToIdTy);
2074 B.CreateCall2(WeakAssignFn, src, dst);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002075}
2076
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002077void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002078 llvm::Value *src, llvm::Value *dst) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002079 CGBuilderTy B = CGF.Builder;
2080 src = EnforceType(B, src, IdTy);
2081 dst = EnforceType(B, dst, PtrToIdTy);
2082 B.CreateCall2(GlobalAssignFn, src, dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002083}
2084
Fariborz Jahaniane881b532008-11-20 19:23:36 +00002085void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00002086 llvm::Value *src, llvm::Value *dst,
2087 llvm::Value *ivarOffset) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002088 CGBuilderTy B = CGF.Builder;
2089 src = EnforceType(B, src, IdTy);
2090 dst = EnforceType(B, dst, PtrToIdTy);
2091 B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00002092}
2093
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002094void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002095 llvm::Value *src, llvm::Value *dst) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002096 CGBuilderTy B = CGF.Builder;
2097 src = EnforceType(B, src, IdTy);
2098 dst = EnforceType(B, dst, PtrToIdTy);
2099 B.CreateCall2(StrongCastAssignFn, src, dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002100}
2101
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002102void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002103 llvm::Value *DestPtr,
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002104 llvm::Value *SrcPtr,
Fariborz Jahanian879d7262009-08-31 19:33:16 +00002105 QualType Ty) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00002106 CGBuilderTy B = CGF.Builder;
2107 DestPtr = EnforceType(B, DestPtr, IdTy);
2108 SrcPtr = EnforceType(B, SrcPtr, PtrToIdTy);
2109
2110 std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty);
2111 unsigned long size = TypeInfo.first/8;
2112 // FIXME: size_t
2113 llvm::Value *N = llvm::ConstantInt::get(LongTy, size);
2114
2115 B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, N);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002116}
2117
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002118llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2119 const ObjCInterfaceDecl *ID,
2120 const ObjCIvarDecl *Ivar) {
2121 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2122 + '.' + Ivar->getNameAsString();
2123 // Emit the variable and initialize it with what we think the correct value
2124 // is. This allows code compiled with non-fragile ivars to work correctly
2125 // when linked against code which isn't (most of the time).
David Chisnall5778fce2009-08-31 16:41:57 +00002126 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2127 if (!IvarOffsetPointer) {
David Chisnall44ec5552010-04-19 01:37:25 +00002128 uint64_t Offset;
2129 if (ObjCImplementationDecl *OID =
Dan Gohman145f3f12010-04-19 16:39:44 +00002130 CGM.getContext().getObjCImplementation(
2131 const_cast<ObjCInterfaceDecl *>(ID)))
David Chisnall44ec5552010-04-19 01:37:25 +00002132 Offset = ComputeIvarBaseOffset(CGM, OID, Ivar);
2133 else
2134 Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2135
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002136 llvm::ConstantInt *OffsetGuess =
David Chisnallc8fc5732010-01-11 19:02:35 +00002137 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
David Chisnall5778fce2009-08-31 16:41:57 +00002138 // Don't emit the guess in non-PIC code because the linker will not be able
2139 // to replace it with the real version for a library. In non-PIC code you
2140 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump11289f42009-09-09 15:08:12 +00002141 // GCC-compiled class.
David Chisnall5778fce2009-08-31 16:41:57 +00002142 if (CGM.getLangOptions().PICLevel) {
2143 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
2144 llvm::Type::getInt32Ty(VMContext), false,
2145 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2146 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2147 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2148 IvarOffsetGV, Name);
2149 } else {
2150 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002151 llvm::Type::getInt32PtrTy(VMContext), false,
2152 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall5778fce2009-08-31 16:41:57 +00002153 }
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002154 }
David Chisnall5778fce2009-08-31 16:41:57 +00002155 return IvarOffsetPointer;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002156}
2157
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00002158LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2159 QualType ObjectTy,
2160 llvm::Value *BaseValue,
2161 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00002162 unsigned CVRQualifiers) {
John McCall9dd450b2009-09-21 23:43:11 +00002163 const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002164 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2165 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002166}
Mike Stumpdd93a192009-07-31 21:31:32 +00002167
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002168static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2169 const ObjCInterfaceDecl *OID,
2170 const ObjCIvarDecl *OIVD) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002171 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002172 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002173 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
2174 if (OIVD == Ivars[k])
2175 return OID;
2176 }
Mike Stump11289f42009-09-09 15:08:12 +00002177
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002178 // Otherwise check in the super class.
2179 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2180 return FindIvarInterface(Context, Super, OIVD);
Mike Stump11289f42009-09-09 15:08:12 +00002181
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002182 return 0;
2183}
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002184
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002185llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00002186 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002187 const ObjCIvarDecl *Ivar) {
David Chisnall5778fce2009-08-31 16:41:57 +00002188 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002189 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall5778fce2009-08-31 16:41:57 +00002190 return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2191 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar"));
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002192 }
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002193 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002194 return llvm::ConstantInt::get(LongTy, Offset, "ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002195}
2196
Mike Stumpdd93a192009-07-31 21:31:32 +00002197CodeGen::CGObjCRuntime *
2198CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) {
Chris Lattner87ab27d2008-06-26 04:19:03 +00002199 return new CGObjCGNU(CGM);
Chris Lattnerb7256cd2008-03-01 08:50:34 +00002200}