blob: 71a01a1c8e1f0648e2ab54818183aaadb97a0f94 [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-03-01 08:50:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000010// This provides Objective-C code generation targetting the GNU runtime. The
11// class in this file generates structures used by the GNU Objective-C runtime
12// library. These structures are defined in objc/objc.h and objc/objc-api.h in
13// the GNU runtime distribution.
Chris Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000020
Chris Lattnerdce14062008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Anders Carlsson3d598a52009-07-14 17:29:11 +000022#include "clang/AST/ASTRecordLayout.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000024#include "clang/AST/DeclObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000026
27#include "llvm/Intrinsics.h"
Chris Lattner0f984262008-03-01 08:50:34 +000028#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000029#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000030#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000031#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000032#include "llvm/Target/TargetData.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000033
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000035
36
Chris Lattnerdce14062008-06-26 04:19:03 +000037using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000038using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000039using llvm::dyn_cast;
40
41// The version of the runtime that this class targets. Must match the version
42// in the runtime.
43static const int RuntimeVersion = 8;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +000044static const int NonFragileRuntimeVersion = 9;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000045static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000046
Chris Lattner0f984262008-03-01 08:50:34 +000047namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000048class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000049private:
Chris Lattnerdce14062008-06-26 04:19:03 +000050 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000051 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000052 const llvm::PointerType *SelectorTy;
53 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000054 const llvm::FunctionType *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000055 const llvm::PointerType *IdTy;
56 const llvm::IntegerType *IntTy;
57 const llvm::PointerType *PtrTy;
58 const llvm::IntegerType *LongTy;
59 const llvm::PointerType *PtrToIntTy;
Daniel Dunbar5efccb12009-05-04 15:31:17 +000060 llvm::GlobalAlias *ClassPtrAlias;
61 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000062 std::vector<llvm::Constant*> Classes;
63 std::vector<llvm::Constant*> Categories;
64 std::vector<llvm::Constant*> ConstantStrings;
65 llvm::Function *LoadFunction;
66 llvm::StringMap<llvm::Constant*> ExistingProtocols;
67 typedef std::pair<std::string, std::string> TypedSelector;
68 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
69 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
70 // Some zeros used for GEPs in lots of places.
71 llvm::Constant *Zeros[2];
72 llvm::Constant *NULLPtr;
Owen Andersona1cf15f2009-07-14 23:10:40 +000073 llvm::LLVMContext &VMContext;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000074private:
75 llvm::Constant *GenerateIvarList(
76 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
78 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
79 llvm::Constant *GenerateMethodList(const std::string &ClassName,
80 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000081 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000082 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
83 bool isClassMethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +000084 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000085 llvm::Constant *GenerateProtocolList(
86 const llvm::SmallVectorImpl<std::string> &Protocols);
87 llvm::Constant *GenerateClassStructure(
88 llvm::Constant *MetaClass,
89 llvm::Constant *SuperClass,
90 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000091 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000092 llvm::Constant *Version,
93 llvm::Constant *InstanceSize,
94 llvm::Constant *IVars,
95 llvm::Constant *Methods,
96 llvm::Constant *Protocols);
97 llvm::Constant *GenerateProtocolMethodList(
98 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
99 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
100 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
101 &Name="");
102 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
103 std::vector<llvm::Constant*> &V, const std::string &Name="");
104 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
105 std::vector<llvm::Constant*> &V, const std::string &Name="");
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000106 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
107 const ObjCIvarDecl *Ivar);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000108 void EmitClassRef(const std::string &className);
Chris Lattner0f984262008-03-01 08:50:34 +0000109public:
Chris Lattnerdce14062008-06-26 04:19:03 +0000110 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff33fdb732009-03-31 16:53:37 +0000111 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000112 virtual CodeGen::RValue
113 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000114 QualType ResultType,
115 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000116 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000117 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000118 const CallArgList &CallArgs,
119 const ObjCMethodDecl *Method);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000120 virtual CodeGen::RValue
121 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000122 QualType ResultType,
123 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000124 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000125 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000126 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000127 bool IsClassMessage,
128 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000129 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000130 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000131 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000132 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
133 *Method);
Chris Lattner8e67b632008-06-26 04:37:12 +0000134
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000135 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
136 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000137 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
138 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000139 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000140 const ObjCProtocolDecl *PD);
141 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000142 virtual llvm::Function *ModuleInitFunction();
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +0000143 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
Daniel Dunbar49f66022008-09-24 03:38:44 +0000144 virtual llvm::Function *GetPropertyGetFunction();
145 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000146 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000147
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000148 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
149 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000150 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
151 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000152 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000153 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000154 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
155 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000156 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
157 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000158 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
159 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000160 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
161 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000162 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
163 llvm::Value *DestPtr,
164 llvm::Value *SrcPtr,
165 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000166 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
167 QualType ObjectTy,
168 llvm::Value *BaseValue,
169 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000170 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000171 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000172 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000173 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000174};
175} // end anonymous namespace
176
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000177
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000178/// Emits a reference to a dummy variable which is emitted with each class.
179/// This ensures that a linker error will be generated when trying to link
180/// together modules where a referenced class is not defined.
181void CGObjCGNU::EmitClassRef(const std::string &className){
182 std::string symbolRef = "__objc_class_ref_" + className;
183 // Don't emit two copies of the same symbol
184 if (TheModule.getGlobalVariable(symbolRef)) return;
185 std::string symbolName = "__objc_class_name_" + className;
186 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
187 if (!ClassSymbol) {
Owen Anderson1c431b32009-07-08 19:05:04 +0000188 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
189 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000190 }
Owen Anderson1c431b32009-07-08 19:05:04 +0000191 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
192 llvm::GlobalValue::CommonLinkage, ClassSymbol, symbolRef);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000193}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000194
195static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000196 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000197}
198
199static std::string SymbolNameForMethod(const std::string &ClassName, const
200 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
201{
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000202 return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000203 (isClassMethod ? "+" : "-") + MethodName;
204}
205
Chris Lattnerdce14062008-06-26 04:19:03 +0000206CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000207 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000208 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000209 IntTy = cast<llvm::IntegerType>(
210 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
211 LongTy = cast<llvm::IntegerType>(
212 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000213
Owen Andersona1cf15f2009-07-14 23:10:40 +0000214 Zeros[0] = VMContext.getConstantInt(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000215 Zeros[1] = Zeros[0];
Owen Andersona1cf15f2009-07-14 23:10:40 +0000216 NULLPtr = VMContext.getConstantPointerNull(
217 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000218 // C string type. Used in lots of places.
219 PtrToInt8Ty =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000220 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Chris Lattner391d77a2008-03-30 23:03:07 +0000221 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000222 SelectorTy = cast<llvm::PointerType>(
223 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
224
Owen Andersona1cf15f2009-07-14 23:10:40 +0000225 PtrToIntTy = VMContext.getPointerTypeUnqual(IntTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000226 PtrTy = PtrToInt8Ty;
227
228 // Object type
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000229 IdTy = cast<llvm::PointerType>(
230 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattner391d77a2008-03-30 23:03:07 +0000231
232 // IMP type
233 std::vector<const llvm::Type*> IMPArgs;
234 IMPArgs.push_back(IdTy);
235 IMPArgs.push_back(SelectorTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000236 IMPTy = VMContext.getFunctionType(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000237}
238// This has to perform the lookup every time, since posing and related
239// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000240llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000241 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000242 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000243 EmitClassRef(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000244 ClassName = Builder.CreateStructGEP(ClassName, 0);
245
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000246 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000247 llvm::Constant *ClassLookupFn =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000248 CGM.CreateRuntimeFunction(VMContext.getFunctionType(IdTy,
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000249 Params,
250 true),
251 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000252 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000253}
254
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000255llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000256 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000257 if (US == 0)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000258 US = new llvm::GlobalAlias(VMContext.getPointerTypeUnqual(SelectorTy),
Chris Lattner8e67b632008-06-26 04:37:12 +0000259 llvm::GlobalValue::InternalLinkage,
260 ".objc_untyped_selector_alias",
261 NULL, &TheModule);
262
263 return Builder.CreateLoad(US);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000264}
265
266llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
267 *Method) {
268
269 std::string SelName = Method->getSelector().getAsString();
270 std::string SelTypes;
271 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
272 // Typed selectors
273 TypedSelector Selector = TypedSelector(SelName,
274 SelTypes);
275
276 // If it's already cached, return it.
277 if (TypedSelectors[Selector])
278 {
279 return Builder.CreateLoad(TypedSelectors[Selector]);
280 }
281
282 // If it isn't, cache it.
283 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000284 VMContext.getPointerTypeUnqual(SelectorTy),
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000285 llvm::GlobalValue::InternalLinkage, SelName,
286 NULL, &TheModule);
287 TypedSelectors[Selector] = Sel;
288
289 return Builder.CreateLoad(Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000290}
291
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000292llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
293 const std::string &Name) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000294 llvm::Constant * ConstStr = VMContext.getConstantArray(Str);
Owen Anderson1c431b32009-07-08 19:05:04 +0000295 ConstStr = new llvm::GlobalVariable(TheModule, ConstStr->getType(), true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000296 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000297 ConstStr, Name);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000298 return VMContext.getConstantExprGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000299}
300llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
301 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000302 llvm::Constant *C = VMContext.getConstantStruct(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000303 return new llvm::GlobalVariable(TheModule, Ty, false,
304 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000305}
306llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
307 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000308 llvm::Constant *C = VMContext.getConstantArray(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000309 return new llvm::GlobalVariable(TheModule, Ty, false,
310 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000311}
312
313/// Generate an NSConstantString object.
314//TODO: In case there are any crazy people still using the GNU runtime without
315//an OpenStep implementation, this should let them select their own class for
316//constant strings.
Steve Naroff33fdb732009-03-31 16:53:37 +0000317llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
318 std::string Str(SL->getString()->getStrData(),
319 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320 std::vector<llvm::Constant*> Ivars;
321 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000322 Ivars.push_back(MakeConstantString(Str));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000323 Ivars.push_back(VMContext.getConstantInt(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000324 llvm::Constant *ObjCStr = MakeGlobal(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000325 VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000326 Ivars, ".objc_str");
327 ConstantStrings.push_back(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000328 VMContext.getConstantExprBitCast(ObjCStr, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329 return ObjCStr;
330}
331
332///Generates a message send where the super is the receiver. This is a message
333///send to self with special delivery semantics indicating which class's method
334///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000335CodeGen::RValue
336CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000337 QualType ResultType,
338 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000339 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000340 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000341 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000342 bool IsClassMessage,
343 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000344 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
345
346 CallArgList ActualArgs;
347
348 ActualArgs.push_back(
349 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
350 CGF.getContext().getObjCIdType()));
351 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
352 CGF.getContext().getObjCSelType()));
353 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
354
355 CodeGenTypes &Types = CGM.getTypes();
356 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
357 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
358
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000359 llvm::Value *ReceiverClass = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000360 if (isCategoryImpl) {
361 llvm::Constant *classLookupFunction = 0;
362 std::vector<const llvm::Type*> Params;
363 Params.push_back(PtrTy);
364 if (IsClassMessage) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000365 classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000366 IdTy, Params, true), "objc_get_meta_class");
367 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000368 classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000369 IdTy, Params, true), "objc_get_class");
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000370 }
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000371 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
372 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000373 } else {
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000374 // Set up global aliases for the metaclass or class pointer if they do not
375 // already exist. These will are forward-references which will be set to
376 // pointers to the class and metaclass structure created for the runtime load
377 // function. To send a message to super, we look up the value of the
378 // super_class pointer from either the class or metaclass structure.
379 if (IsClassMessage) {
380 if (!MetaClassPtrAlias) {
381 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
382 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
383 Class->getNameAsString(), NULL, &TheModule);
384 }
385 ReceiverClass = MetaClassPtrAlias;
386 } else {
387 if (!ClassPtrAlias) {
388 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
389 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
390 Class->getNameAsString(), NULL, &TheModule);
391 }
392 ReceiverClass = ClassPtrAlias;
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000393 }
Chris Lattner71238f62009-04-25 23:19:45 +0000394 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000395 // Cast the pointer to a simplified version of the class structure
396 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 VMContext.getPointerTypeUnqual(
398 VMContext.getStructType(IdTy, IdTy, NULL)));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000399 // Get the superclass pointer
400 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
401 // Load the superclass pointer
402 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000403 // Construct the structure used to look up the IMP
Owen Andersona1cf15f2009-07-14 23:10:40 +0000404 llvm::StructType *ObjCSuperTy = VMContext.getStructType(Receiver->getType(),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000405 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000406 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000407
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000408 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000409 CGF.Builder.CreateStore(ReceiverClass,
410 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000411
412 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000413 std::vector<const llvm::Type*> Params;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000414 Params.push_back(VMContext.getPointerTypeUnqual(ObjCSuperTy));
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000415 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000416 llvm::Constant *lookupFunction =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000417 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
418 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000419 "objc_msg_lookup_super");
420
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000421 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000422 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000423 lookupArgs+2);
424
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000425 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000426}
427
428/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000429CodeGen::RValue
430CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000431 QualType ResultType,
432 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000433 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000434 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000435 const CallArgList &CallArgs,
436 const ObjCMethodDecl *Method) {
437 llvm::Value *cmd;
438 if (Method)
439 cmd = GetSelector(CGF.Builder, Method);
440 else
441 cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000442 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000443
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000444 ActualArgs.push_back(
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000445 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
446 CGF.getContext().getObjCIdType()));
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000447 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
448 CGF.getContext().getObjCSelType()));
449 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
450
451 CodeGenTypes &Types = CGM.getTypes();
452 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
453 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
454
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000455 llvm::Value *imp;
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000456 std::vector<const llvm::Type*> Params;
457 Params.push_back(Receiver->getType());
458 Params.push_back(SelectorTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000459 // For sender-aware dispatch, we pass the sender as the third argument to a
460 // lookup function. When sending messages from C code, the sender is nil.
461 // objc_msg_lookup_sender(id receiver, SEL selector, id sender);
462 if (CGM.getContext().getLangOptions().ObjCSenderDispatch) {
463 llvm::Value *self;
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000464
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000465 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
466 self = CGF.LoadObjCSelf();
467 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000468 self = VMContext.getConstantPointerNull(IdTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000469 }
470 Params.push_back(self->getType());
471 llvm::Constant *lookupFunction =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000472 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
473 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000474 "objc_msg_lookup_sender");
475
476 imp = CGF.Builder.CreateCall3(lookupFunction, Receiver, cmd, self);
477 } else {
478 llvm::Constant *lookupFunction =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000479 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
480 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000481 "objc_msg_lookup");
482
483 imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
484 }
Chris Lattner3eae03e2008-05-06 00:56:42 +0000485
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000486 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000487}
488
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000489/// Generates a MethodList. Used in construction of a objc_class and
490/// objc_category structures.
491llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000492 const std::string &CategoryName,
493 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000494 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
495 bool isClassMethodList) {
496 // Get the method structure type.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000497 llvm::StructType *ObjCMethodTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000498 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
499 PtrToInt8Ty, // Method types
Owen Andersona1cf15f2009-07-14 23:10:40 +0000500 VMContext.getPointerTypeUnqual(IMPTy), //Method pointer
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000501 NULL);
502 std::vector<llvm::Constant*> Methods;
503 std::vector<llvm::Constant*> Elements;
504 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
505 Elements.clear();
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000506 if (llvm::Constant *Method =
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000507 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000508 MethodSels[i].getAsString(),
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000509 isClassMethodList))) {
510 llvm::Constant *C =
511 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000512 Elements.push_back(VMContext.getConstantExprGetElementPtr(C, Zeros, 2));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000513 Elements.push_back(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000514 VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2));
515 Method = VMContext.getConstantExprBitCast(Method,
516 VMContext.getPointerTypeUnqual(IMPTy));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000517 Elements.push_back(Method);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000518 Methods.push_back(VMContext.getConstantStruct(ObjCMethodTy, Elements));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000519 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000520 }
521
522 // Array of method structures
Owen Andersona1cf15f2009-07-14 23:10:40 +0000523 llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodTy,
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000524 Methods.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000525 llvm::Constant *MethodArray = VMContext.getConstantArray(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000526 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000527
528 // Structure containing list pointer, array and array count
529 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000530 llvm::PATypeHolder OpaqueNextTy = VMContext.getOpaqueType();
531 llvm::Type *NextPtrTy = VMContext.getPointerTypeUnqual(OpaqueNextTy);
532 llvm::StructType *ObjCMethodListTy = VMContext.getStructType(NextPtrTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000533 IntTy,
534 ObjCMethodArrayTy,
535 NULL);
536 // Refine next pointer type to concrete type
537 llvm::cast<llvm::OpaqueType>(
538 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
539 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
540
541 Methods.clear();
Owen Andersona1cf15f2009-07-14 23:10:40 +0000542 Methods.push_back(VMContext.getConstantPointerNull(
543 VMContext.getPointerTypeUnqual(ObjCMethodListTy)));
544 Methods.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000545 MethodTypes.size()));
546 Methods.push_back(MethodArray);
547
548 // Create an instance of the structure
549 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
550}
551
552/// Generates an IvarList. Used in construction of a objc_class.
553llvm::Constant *CGObjCGNU::GenerateIvarList(
554 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
555 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
556 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
557 // Get the method structure type.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000558 llvm::StructType *ObjCIvarTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000559 PtrToInt8Ty,
560 PtrToInt8Ty,
561 IntTy,
562 NULL);
563 std::vector<llvm::Constant*> Ivars;
564 std::vector<llvm::Constant*> Elements;
565 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
566 Elements.clear();
Owen Andersona1cf15f2009-07-14 23:10:40 +0000567 Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarNames[i],
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000568 Zeros, 2));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000569 Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarTypes[i],
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000570 Zeros, 2));
571 Elements.push_back(IvarOffsets[i]);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000572 Ivars.push_back(VMContext.getConstantStruct(ObjCIvarTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000573 }
574
575 // Array of method structures
Owen Andersona1cf15f2009-07-14 23:10:40 +0000576 llvm::ArrayType *ObjCIvarArrayTy = VMContext.getArrayType(ObjCIvarTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000577 IvarNames.size());
578
579
580 Elements.clear();
Owen Andersona1cf15f2009-07-14 23:10:40 +0000581 Elements.push_back(VMContext.getConstantInt(IntTy, (int)IvarNames.size()));
582 Elements.push_back(VMContext.getConstantArray(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000583 // Structure containing array and array count
Owen Andersona1cf15f2009-07-14 23:10:40 +0000584 llvm::StructType *ObjCIvarListTy = VMContext.getStructType(IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000585 ObjCIvarArrayTy,
586 NULL);
587
588 // Create an instance of the structure
589 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
590}
591
592/// Generate a class structure
593llvm::Constant *CGObjCGNU::GenerateClassStructure(
594 llvm::Constant *MetaClass,
595 llvm::Constant *SuperClass,
596 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000597 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000598 llvm::Constant *Version,
599 llvm::Constant *InstanceSize,
600 llvm::Constant *IVars,
601 llvm::Constant *Methods,
602 llvm::Constant *Protocols) {
603 // Set up the class structure
604 // Note: Several of these are char*s when they should be ids. This is
605 // because the runtime performs this translation on load.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000606 llvm::StructType *ClassTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000607 PtrToInt8Ty, // class_pointer
608 PtrToInt8Ty, // super_class
609 PtrToInt8Ty, // name
610 LongTy, // version
611 LongTy, // info
612 LongTy, // instance_size
613 IVars->getType(), // ivars
614 Methods->getType(), // methods
615 // These are all filled in by the runtime, so we pretend
616 PtrTy, // dtable
617 PtrTy, // subclass_list
618 PtrTy, // sibling_class
619 PtrTy, // protocols
620 PtrTy, // gc_object_type
621 NULL);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000622 llvm::Constant *Zero = VMContext.getConstantInt(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000623 llvm::Constant *NullP =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000624 VMContext.getConstantPointerNull(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000625 // Fill in the structure
626 std::vector<llvm::Constant*> Elements;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000627 Elements.push_back(VMContext.getConstantExprBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000628 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000629 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000630 Elements.push_back(Zero);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000631 Elements.push_back(VMContext.getConstantInt(LongTy, info));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000632 Elements.push_back(InstanceSize);
633 Elements.push_back(IVars);
634 Elements.push_back(Methods);
635 Elements.push_back(NullP);
636 Elements.push_back(NullP);
637 Elements.push_back(NullP);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000638 Elements.push_back(VMContext.getConstantExprBitCast(Protocols, PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000639 Elements.push_back(NullP);
640 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000641 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000642}
643
644llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
645 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
646 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
647 // Get the method structure type.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000648 llvm::StructType *ObjCMethodDescTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000649 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
650 PtrToInt8Ty,
651 NULL);
652 std::vector<llvm::Constant*> Methods;
653 std::vector<llvm::Constant*> Elements;
654 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
655 Elements.clear();
Owen Andersona1cf15f2009-07-14 23:10:40 +0000656 Elements.push_back(VMContext.getConstantExprGetElementPtr(MethodNames[i],
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000657 Zeros, 2));
658 Elements.push_back(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000659 VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2));
660 Methods.push_back(VMContext.getConstantStruct(ObjCMethodDescTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000661 }
Owen Andersona1cf15f2009-07-14 23:10:40 +0000662 llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodDescTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000663 MethodNames.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000664 llvm::Constant *Array = VMContext.getConstantArray(ObjCMethodArrayTy,
665 Methods);
666 llvm::StructType *ObjCMethodDescListTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000667 IntTy, ObjCMethodArrayTy, NULL);
668 Methods.clear();
Owen Andersona1cf15f2009-07-14 23:10:40 +0000669 Methods.push_back(VMContext.getConstantInt(IntTy, MethodNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000670 Methods.push_back(Array);
671 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
672}
673// Create the protocol list structure used in classes, categories and so on
674llvm::Constant *CGObjCGNU::GenerateProtocolList(
675 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000676 llvm::ArrayType *ProtocolArrayTy = VMContext.getArrayType(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000677 Protocols.size());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000678 llvm::StructType *ProtocolListTy = VMContext.getStructType(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000679 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
680 LongTy,//FIXME: Should be size_t
681 ProtocolArrayTy,
682 NULL);
683 std::vector<llvm::Constant*> Elements;
684 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
685 iter != endIter ; iter++) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000686 llvm::Constant *protocol = ExistingProtocols[*iter];
687 if (!protocol)
688 protocol = GenerateEmptyProtocol(*iter);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000689 llvm::Constant *Ptr = VMContext.getConstantExprBitCast(protocol,
690 PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000691 Elements.push_back(Ptr);
692 }
Owen Andersona1cf15f2009-07-14 23:10:40 +0000693 llvm::Constant * ProtocolArray = VMContext.getConstantArray(ProtocolArrayTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000694 Elements);
695 Elements.clear();
696 Elements.push_back(NULLPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000697 Elements.push_back(VMContext.getConstantInt(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000698 Elements.push_back(ProtocolArray);
699 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
700}
701
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000702llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000703 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000704 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
705 const llvm::Type *T =
706 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000707 return Builder.CreateBitCast(protocol, VMContext.getPointerTypeUnqual(T));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000708}
709
710llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
711 const std::string &ProtocolName) {
712 llvm::SmallVector<std::string, 0> EmptyStringVector;
713 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
714
715 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
716 llvm::Constant *InstanceMethodList =
717 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
718 llvm::Constant *ClassMethodList =
719 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
720 // Protocols are objects containing lists of the methods implemented and
721 // protocols adopted.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000722 llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy,
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000723 PtrToInt8Ty,
724 ProtocolList->getType(),
725 InstanceMethodList->getType(),
726 ClassMethodList->getType(),
727 NULL);
728 std::vector<llvm::Constant*> Elements;
729 // The isa pointer must be set to a magic number so the runtime knows it's
730 // the correct layout.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000731 Elements.push_back(VMContext.getConstantExprIntToPtr(
732 VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000733 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
734 Elements.push_back(ProtocolList);
735 Elements.push_back(InstanceMethodList);
736 Elements.push_back(ClassMethodList);
737 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000738}
739
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000740void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
741 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000742 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000743 llvm::SmallVector<std::string, 16> Protocols;
744 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
745 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000746 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000747 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
748 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000749 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
750 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000751 std::string TypeStr;
752 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
753 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000754 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000755 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000756 }
757 // Collect information about class methods:
758 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
759 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000760 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000761 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
762 iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000763 std::string TypeStr;
764 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
765 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000766 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000767 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000768 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769
770 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
771 llvm::Constant *InstanceMethodList =
772 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
773 llvm::Constant *ClassMethodList =
774 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
775 // Protocols are objects containing lists of the methods implemented and
776 // protocols adopted.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000777 llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000778 PtrToInt8Ty,
779 ProtocolList->getType(),
780 InstanceMethodList->getType(),
781 ClassMethodList->getType(),
782 NULL);
783 std::vector<llvm::Constant*> Elements;
784 // The isa pointer must be set to a magic number so the runtime knows it's
785 // the correct layout.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000786 Elements.push_back(VMContext.getConstantExprIntToPtr(
787 VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000788 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
789 Elements.push_back(ProtocolList);
790 Elements.push_back(InstanceMethodList);
791 Elements.push_back(ClassMethodList);
792 ExistingProtocols[ProtocolName] =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000793 VMContext.getConstantExprBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000794 ".objc_protocol"), IdTy);
795}
796
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000797void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000798 std::string ClassName = OCD->getClassInterface()->getNameAsString();
799 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000800 // Collect information about instance methods
801 llvm::SmallVector<Selector, 16> InstanceMethodSels;
802 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000803 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000804 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000805 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000806 InstanceMethodSels.push_back((*iter)->getSelector());
807 std::string TypeStr;
808 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
809 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
810 }
811
812 // Collect information about class methods
813 llvm::SmallVector<Selector, 16> ClassMethodSels;
814 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000815 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000817 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000818 ClassMethodSels.push_back((*iter)->getSelector());
819 std::string TypeStr;
820 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
821 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
822 }
823
824 // Collect the names of referenced protocols
825 llvm::SmallVector<std::string, 16> Protocols;
826 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
827 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
828 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
829 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000830 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000831
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000832 std::vector<llvm::Constant*> Elements;
833 Elements.push_back(MakeConstantString(CategoryName));
834 Elements.push_back(MakeConstantString(ClassName));
835 // Instance method list
Owen Andersona1cf15f2009-07-14 23:10:40 +0000836 Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000837 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 false), PtrTy));
839 // Class method list
Owen Andersona1cf15f2009-07-14 23:10:40 +0000840 Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000841 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000842 PtrTy));
843 // Protocol list
Owen Andersona1cf15f2009-07-14 23:10:40 +0000844 Elements.push_back(VMContext.getConstantExprBitCast(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000845 GenerateProtocolList(Protocols), PtrTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000846 Categories.push_back(VMContext.getConstantExprBitCast(
847 MakeGlobal(VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000848 PtrTy, PtrTy, NULL), Elements), PtrTy));
849}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000850
851void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
852 ASTContext &Context = CGM.getContext();
853
854 // Get the superclass name.
855 const ObjCInterfaceDecl * SuperClassDecl =
856 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000857 std::string SuperClassName;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000858 if (SuperClassDecl) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000859 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000860 EmitClassRef(SuperClassName);
861 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000862
863 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +0000864 ObjCInterfaceDecl *ClassDecl =
865 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000866 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000867 // Emit the symbol that is used to generate linker errors if this class is
868 // referenced in other modules but not declared.
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000869 std::string classSymbolName = "__objc_class_name_" + ClassName;
870 if (llvm::GlobalVariable *symbol =
871 TheModule.getGlobalVariable(classSymbolName)) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000872 symbol->setInitializer(VMContext.getConstantInt(LongTy, 0));
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000873 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +0000874 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000875 llvm::GlobalValue::ExternalLinkage, VMContext.getConstantInt(LongTy, 0),
Owen Anderson1c431b32009-07-08 19:05:04 +0000876 classSymbolName);
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000877 }
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000878
Daniel Dunbar2bebbf02009-05-03 10:46:44 +0000879 // Get the size of instances.
880 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000881
882 // Collect information about instance variables.
883 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
884 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
885 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000886
887 int superInstanceSize = !SuperClassDecl ? 0 :
888 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
889 // For non-fragile ivars, set the instance size to 0 - {the size of just this
890 // class}. The runtime will then set this to the correct value on load.
891 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
892 instanceSize = 0 - (instanceSize - superInstanceSize);
893 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000894 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
895 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
896 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000897 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
898 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000899 // Get the type encoding for this ivar
900 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000901 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000902 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
903 // Get the offset
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000904 uint64_t Offset;
905 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
906 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter) -
907 superInstanceSize;
908 ObjCIvarOffsetVariable(ClassDecl, *iter);
909 } else {
910 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
911 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000912 IvarOffsets.push_back(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000913 VMContext.getConstantInt(llvm::Type::Int32Ty, Offset));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000914 }
915
916 // Collect information about instance methods
917 llvm::SmallVector<Selector, 16> InstanceMethodSels;
918 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000919 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000920 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000921 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000922 InstanceMethodSels.push_back((*iter)->getSelector());
923 std::string TypeStr;
924 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
925 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
926 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000927 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000928 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000929 iter != endIter ; iter++) {
930 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
931 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
932 InstanceMethodSels.push_back(getter->getSelector());
933 std::string TypeStr;
934 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
935 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
936 }
937 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
938 InstanceMethodSels.push_back(setter->getSelector());
939 std::string TypeStr;
940 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
941 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
942 }
943 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000944
945 // Collect information about class methods
946 llvm::SmallVector<Selector, 16> ClassMethodSels;
947 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000948 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000949 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000950 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000951 ClassMethodSels.push_back((*iter)->getSelector());
952 std::string TypeStr;
953 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
954 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
955 }
956 // Collect the names of referenced protocols
957 llvm::SmallVector<std::string, 16> Protocols;
958 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
959 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
960 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000961 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000962
963
964
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000965 // Get the superclass pointer.
966 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000967 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000968 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
969 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000970 SuperClass = VMContext.getConstantPointerNull(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000971 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000972 // Empty vector used to construct empty method lists
973 llvm::SmallVector<llvm::Constant*, 1> empty;
974 // Generate the method and instance variable lists
975 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000976 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000977 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000978 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000979 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
980 IvarOffsets);
981 //Generate metaclass for class methods
982 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000983 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000984 empty, empty, empty), ClassMethodList, NULLPtr);
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000985
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000986 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000987 llvm::Constant *ClassStruct =
988 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
989 ClassName.c_str(), 0,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000990 VMContext.getConstantInt(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000991 MethodList, GenerateProtocolList(Protocols));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000992
993 // Resolve the class aliases, if they exist.
994 if (ClassPtrAlias) {
995 ClassPtrAlias->setAliasee(
Owen Andersona1cf15f2009-07-14 23:10:40 +0000996 VMContext.getConstantExprBitCast(ClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000997 ClassPtrAlias = 0;
998 }
999 if (MetaClassPtrAlias) {
1000 MetaClassPtrAlias->setAliasee(
Owen Andersona1cf15f2009-07-14 23:10:40 +00001001 VMContext.getConstantExprBitCast(MetaClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001002 MetaClassPtrAlias = 0;
1003 }
1004
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001005 // Add class structure to list to be added to the symtab later
Owen Andersona1cf15f2009-07-14 23:10:40 +00001006 ClassStruct = VMContext.getConstantExprBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001007 Classes.push_back(ClassStruct);
1008}
1009
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00001010void CGObjCGNU::MergeMetadataGlobals(
1011 std::vector<llvm::Constant*> &UsedArray) {
1012}
1013
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001014llvm::Function *CGObjCGNU::ModuleInitFunction() {
1015 // Only emit an ObjC load function if no Objective-C stuff has been called
1016 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1017 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +00001018 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001019 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +00001020
Chris Lattnere160c9b2009-01-27 05:06:01 +00001021 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1022 SelectorTy->getElementType());
1023 const llvm::Type *SelStructPtrTy = SelectorTy;
1024 bool isSelOpaque = false;
1025 if (SelStructTy == 0) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001026 SelStructTy = VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, NULL);
1027 SelStructPtrTy = VMContext.getPointerTypeUnqual(SelStructTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +00001028 isSelOpaque = true;
1029 }
1030
Eli Friedman1b8956e2008-06-01 16:00:02 +00001031 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +00001032 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +00001033 TheModule.addTypeName(".objc_id", IdTy);
1034 TheModule.addTypeName(".objc_imp", IMPTy);
1035
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001036 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +00001037 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001038 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +00001039 if (ConstantStrings.size()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001040 llvm::ArrayType *StaticsArrayTy = VMContext.getArrayType(PtrToInt8Ty,
Chris Lattner71238f62009-04-25 23:19:45 +00001041 ConstantStrings.size() + 1);
1042 ConstantStrings.push_back(NULLPtr);
1043 Elements.push_back(MakeConstantString("NSConstantString",
1044 ".objc_static_class_name"));
Owen Andersona1cf15f2009-07-14 23:10:40 +00001045 Elements.push_back(VMContext.getConstantArray(StaticsArrayTy,
Chris Lattner71238f62009-04-25 23:19:45 +00001046 ConstantStrings));
1047 llvm::StructType *StaticsListTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001048 VMContext.getStructType(PtrToInt8Ty, StaticsArrayTy, NULL);
1049 llvm::Type *StaticsListPtrTy =
1050 VMContext.getPointerTypeUnqual(StaticsListTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001051 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001052 llvm::ArrayType *StaticsListArrayTy =
1053 VMContext.getArrayType(StaticsListPtrTy, 2);
Chris Lattner71238f62009-04-25 23:19:45 +00001054 Elements.clear();
1055 Elements.push_back(Statics);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001056 Elements.push_back(VMContext.getNullValue(StaticsListPtrTy));
Chris Lattner71238f62009-04-25 23:19:45 +00001057 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001058 Statics = VMContext.getConstantExprBitCast(Statics, PtrTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001059 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001060 // Array of classes, categories, and constant objects
Owen Andersona1cf15f2009-07-14 23:10:40 +00001061 llvm::ArrayType *ClassListTy = VMContext.getArrayType(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001062 Classes.size() + Categories.size() + 2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001063 llvm::StructType *SymTabTy = VMContext.getStructType(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +00001064 llvm::Type::Int16Ty,
1065 llvm::Type::Int16Ty,
1066 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001067
1068 Elements.clear();
1069 // Pointer to an array of selectors used in this module.
1070 std::vector<llvm::Constant*> Selectors;
1071 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1072 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1073 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +00001074 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
1075 Elements.push_back(MakeConstantString(iter->first.second,
1076 ".objc_sel_types"));
Owen Andersona1cf15f2009-07-14 23:10:40 +00001077 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001078 Elements.clear();
1079 }
1080 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1081 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +00001082 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001083 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +00001084 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001085 Elements.push_back(NULLPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001086 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001087 Elements.clear();
1088 }
1089 Elements.push_back(NULLPtr);
1090 Elements.push_back(NULLPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001091 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001092 Elements.clear();
1093 // Number of static selectors
Owen Andersona1cf15f2009-07-14 23:10:40 +00001094 Elements.push_back(VMContext.getConstantInt(LongTy, Selectors.size() ));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001095 llvm::Constant *SelectorList = MakeGlobal(
Owen Andersona1cf15f2009-07-14 23:10:40 +00001096 VMContext.getArrayType(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001097 ".objc_selector_list");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001098 Elements.push_back(VMContext.getConstantExprBitCast(SelectorList,
Chris Lattnere160c9b2009-01-27 05:06:01 +00001099 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001100
1101 // Now that all of the static selectors exist, create pointers to them.
1102 int index = 0;
1103 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1104 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1105 iter != iterEnd; ++iter) {
1106 llvm::Constant *Idxs[] = {Zeros[0],
Owen Andersona1cf15f2009-07-14 23:10:40 +00001107 VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]};
Owen Anderson1c431b32009-07-08 19:05:04 +00001108 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1109 true, llvm::GlobalValue::InternalLinkage,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001110 VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson1c431b32009-07-08 19:05:04 +00001111 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001112 // If selectors are defined as an opaque type, cast the pointer to this
1113 // type.
1114 if (isSelOpaque) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001115 SelPtr = VMContext.getConstantExprBitCast(SelPtr,
1116 VMContext.getPointerTypeUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001117 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001118 (*iter).second->setAliasee(SelPtr);
1119 }
1120 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1121 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1122 iter != iterEnd; iter++) {
1123 llvm::Constant *Idxs[] = {Zeros[0],
Owen Andersona1cf15f2009-07-14 23:10:40 +00001124 VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]};
Owen Anderson1c431b32009-07-08 19:05:04 +00001125 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1126 true, llvm::GlobalValue::InternalLinkage,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001127 VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson1c431b32009-07-08 19:05:04 +00001128 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001129 // If selectors are defined as an opaque type, cast the pointer to this
1130 // type.
1131 if (isSelOpaque) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001132 SelPtr = VMContext.getConstantExprBitCast(SelPtr,
1133 VMContext.getPointerTypeUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001134 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001135 (*iter).second->setAliasee(SelPtr);
1136 }
1137 // Number of classes defined.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001138 Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001139 Classes.size()));
1140 // Number of categories defined
Owen Andersona1cf15f2009-07-14 23:10:40 +00001141 Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001142 Categories.size()));
1143 // Create an array of classes, then categories, then static object instances
1144 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1145 // NULL-terminated list of static object instances (mainly constant strings)
1146 Classes.push_back(Statics);
1147 Classes.push_back(NULLPtr);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001148 llvm::Constant *ClassList = VMContext.getConstantArray(ClassListTy, Classes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001149 Elements.push_back(ClassList);
1150 // Construct the symbol table
1151 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1152
1153 // The symbol table is contained in a module which has some version-checking
1154 // constants
Owen Andersona1cf15f2009-07-14 23:10:40 +00001155 llvm::StructType * ModuleTy = VMContext.getStructType(LongTy, LongTy,
1156 PtrToInt8Ty, VMContext.getPointerTypeUnqual(SymTabTy), NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001157 Elements.clear();
1158 // Runtime version used for compatibility checking.
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001159 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001160 Elements.push_back(VMContext.getConstantInt(LongTy,
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001161 NonFragileRuntimeVersion));
1162 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001163 Elements.push_back(VMContext.getConstantInt(LongTy, RuntimeVersion));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001164 }
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00001165 // sizeof(ModuleTy)
1166 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001167 Elements.push_back(VMContext.getConstantInt(LongTy,
1168 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001169 //FIXME: Should be the path to the file where this module was declared
1170 Elements.push_back(NULLPtr);
1171 Elements.push_back(SymTab);
1172 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1173
1174 // Create the load function calling the runtime entry point with the module
1175 // structure
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001176 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Andersona1cf15f2009-07-14 23:10:40 +00001177 VMContext.getFunctionType(llvm::Type::VoidTy, false),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001178 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1179 &TheModule);
1180 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001181 CGBuilderTy Builder(VMContext);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001182 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001183
1184 std::vector<const llvm::Type*> Params(1,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001185 VMContext.getPointerTypeUnqual(ModuleTy));
1186 llvm::Value *Register = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001187 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001188 Builder.CreateCall(Register, Module);
1189 Builder.CreateRetVoid();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001190
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001191 return LoadFunction;
1192}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001193
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001194llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1195 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001196 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001197 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001198 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1199 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1200 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001201 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001202
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001203 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001204 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001205 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001206 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1207 MethodName, isClassMethod);
1208
Gabor Greif984d0b42008-04-06 20:42:52 +00001209 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +00001210 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001211 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +00001212 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001213 return Method;
1214}
1215
Daniel Dunbar49f66022008-09-24 03:38:44 +00001216llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001217 std::vector<const llvm::Type*> Params;
1218 const llvm::Type *BoolTy =
1219 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1220 Params.push_back(IdTy);
1221 Params.push_back(SelectorTy);
1222 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1223 Params.push_back(LongTy);
1224 Params.push_back(BoolTy);
1225 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1226 const llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001227 VMContext.getFunctionType(IdTy, Params, false);
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001228 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1229 "objc_getProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001230}
1231
1232llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001233 std::vector<const llvm::Type*> Params;
1234 const llvm::Type *BoolTy =
1235 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1236 Params.push_back(IdTy);
1237 Params.push_back(SelectorTy);
1238 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1239 Params.push_back(LongTy);
1240 Params.push_back(IdTy);
1241 Params.push_back(BoolTy);
1242 Params.push_back(BoolTy);
1243 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1244 const llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001245 VMContext.getFunctionType(llvm::Type::VoidTy, Params, false);
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001246 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1247 "objc_setProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001248}
1249
1250llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001251 std::vector<const llvm::Type*> Params(1, IdTy);
1252 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
Owen Andersona1cf15f2009-07-14 23:10:40 +00001253 VMContext.getFunctionType(llvm::Type::VoidTy, Params, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001254 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001255}
1256
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001257void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1258 const Stmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001259 // Pointer to the personality function
1260 llvm::Constant *Personality =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001261 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattnerb59761b2009-07-01 04:13:52 +00001262 true),
Chris Lattner5dc08672009-05-08 00:11:50 +00001263 "__gnu_objc_personality_v0");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001264 Personality = VMContext.getConstantExprBitCast(Personality, PtrTy);
Chris Lattner5dc08672009-05-08 00:11:50 +00001265 std::vector<const llvm::Type*> Params;
1266 Params.push_back(PtrTy);
1267 llvm::Value *RethrowFn =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001268 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattner5dc08672009-05-08 00:11:50 +00001269 Params, false), "_Unwind_Resume_or_Rethrow");
1270
1271 bool isTry = isa<ObjCAtTryStmt>(S);
1272 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1273 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1274 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattnerd6a99072009-05-11 18:16:28 +00001275 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattner5dc08672009-05-08 00:11:50 +00001276 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1277 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1278 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1279
1280 // GNU runtime does not currently support @synchronized()
1281 if (!isTry) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001282 std::vector<const llvm::Type*> Args(1, IdTy);
1283 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001284 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001285 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
1286 llvm::Value *SyncArg =
1287 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1288 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1289 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001290 }
1291
Chris Lattnerd6a99072009-05-11 18:16:28 +00001292
Chris Lattner5dc08672009-05-08 00:11:50 +00001293 // Push an EH context entry, used for handling rethrows and jumps
1294 // through finally.
1295 CGF.PushCleanupBlock(FinallyBlock);
1296
1297 // Emit the statements in the @try {} block
1298 CGF.setInvokeDest(TryHandler);
1299
1300 CGF.EmitBlock(TryBlock);
1301 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1302 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1303
1304 // Jump to @finally if there is no exception
1305 CGF.EmitBranchThroughCleanup(FinallyEnd);
1306
1307 // Emit the handlers
1308 CGF.EmitBlock(TryHandler);
1309
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001310 // Get the correct versions of the exception handling intrinsics
1311 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1312 int PointerWidth = td.getTypeSizeInBits(PtrTy);
1313 assert((PointerWidth == 32 || PointerWidth == 64) &&
1314 "Can't yet handle exceptions if pointers are not 32 or 64 bits");
Chris Lattner5dc08672009-05-08 00:11:50 +00001315 llvm::Value *llvm_eh_exception =
1316 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001317 llvm::Value *llvm_eh_selector = PointerWidth == 32 ?
1318 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i32) :
1319 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
1320 llvm::Value *llvm_eh_typeid_for = PointerWidth == 32 ?
1321 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i32) :
1322 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
Chris Lattner5dc08672009-05-08 00:11:50 +00001323
1324 // Exception object
1325 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1326 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1327
1328 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1329 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1330
1331 ESelArgs.push_back(Exc);
1332 ESelArgs.push_back(Personality);
1333
1334 bool HasCatchAll = false;
1335 // Only @try blocks are allowed @catch blocks, but both can have @finally
1336 if (isTry) {
1337 if (const ObjCAtCatchStmt* CatchStmt =
1338 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001339 CGF.setInvokeDest(CatchInCatch);
Chris Lattner5dc08672009-05-08 00:11:50 +00001340
1341 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1342 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
1343 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
1344
1345 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff14108da2009-07-10 23:34:53 +00001346 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattner5dc08672009-05-08 00:11:50 +00001347 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1348 // Use i8* null here to signal this is a catch all, not a cleanup.
1349 ESelArgs.push_back(NULLPtr);
1350 HasCatchAll = true;
1351 // No further catches after this one will ever by reached
1352 break;
1353 }
1354
1355 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00001356 const ObjCObjectPointerType *OPT =
1357 CatchDecl->getType()->getAsObjCObjectPointerType();
1358 assert(OPT && "Invalid @catch type.");
Chris Lattner5dc08672009-05-08 00:11:50 +00001359 const ObjCInterfaceType *IT =
Steve Naroff14108da2009-07-10 23:34:53 +00001360 OPT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner5dc08672009-05-08 00:11:50 +00001361 assert(IT && "Invalid @catch type.");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001362 llvm::Value *EHType =
1363 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattner5dc08672009-05-08 00:11:50 +00001364 ESelArgs.push_back(EHType);
1365 }
1366 }
1367 }
1368
1369 // We use a cleanup unless there was already a catch all.
1370 if (!HasCatchAll) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001371 ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Chris Lattner5dc08672009-05-08 00:11:50 +00001372 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1373 }
1374
1375 // Find which handler was matched.
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001376 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattner5dc08672009-05-08 00:11:50 +00001377 ESelArgs.begin(), ESelArgs.end(), "selector");
1378
1379 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1380 const ParmVarDecl *CatchParam = Handlers[i].first;
1381 const Stmt *CatchBody = Handlers[i].second;
1382
1383 llvm::BasicBlock *Next = 0;
1384
1385 // The last handler always matches.
1386 if (i + 1 != e) {
1387 assert(CatchParam && "Only last handler can be a catch all.");
1388
1389 // Test whether this block matches the type for the selector and branch
1390 // to Match if it does, or to the next BB if it doesn't.
1391 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1392 Next = CGF.createBasicBlock("catch.next");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001393 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattner5dc08672009-05-08 00:11:50 +00001394 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1395 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1396 Next);
1397
1398 CGF.EmitBlock(Match);
1399 }
1400
1401 if (CatchBody) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001402 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1403 CGF.ConvertType(CatchParam->getType()));
1404
1405 // Bind the catch parameter if it exists.
1406 if (CatchParam) {
1407 // CatchParam is a ParmVarDecl because of the grammar
1408 // construction used to handle this, but for codegen purposes
1409 // we treat this as a local decl.
1410 CGF.EmitLocalBlockVarDecl(*CatchParam);
1411 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1412 }
1413
1414 CGF.ObjCEHValueStack.push_back(ExcObject);
1415 CGF.EmitStmt(CatchBody);
1416 CGF.ObjCEHValueStack.pop_back();
1417
1418 CGF.EmitBranchThroughCleanup(FinallyEnd);
1419
1420 if (Next)
1421 CGF.EmitBlock(Next);
1422 } else {
1423 assert(!Next && "catchup should be last handler.");
1424
1425 CGF.Builder.CreateStore(Exc, RethrowPtr);
1426 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1427 }
1428 }
Chris Lattnerd6a99072009-05-11 18:16:28 +00001429 // The @finally block is a secondary landing pad for any exceptions thrown in
1430 // @catch() blocks
1431 CGF.EmitBlock(CatchInCatch);
1432 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1433 ESelArgs.clear();
1434 ESelArgs.push_back(Exc);
1435 ESelArgs.push_back(Personality);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001436 ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Chris Lattnerd6a99072009-05-11 18:16:28 +00001437 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1438 "selector");
1439 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1440 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1441 CGF.Builder.CreateStore(Exc, RethrowPtr);
1442 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1443
Chris Lattner5dc08672009-05-08 00:11:50 +00001444 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1445
1446 CGF.setInvokeDest(PrevLandingPad);
1447
1448 CGF.EmitBlock(FinallyBlock);
1449
Chris Lattnerd6a99072009-05-11 18:16:28 +00001450
Chris Lattner5dc08672009-05-08 00:11:50 +00001451 if (isTry) {
1452 if (const ObjCAtFinallyStmt* FinallyStmt =
1453 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1454 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1455 } else {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001456 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattner5dc08672009-05-08 00:11:50 +00001457 // @synchronized.
Chris Lattnerd6a99072009-05-11 18:16:28 +00001458 std::vector<const llvm::Type*> Args(1, IdTy);
1459 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001460 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001461 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
1462 llvm::Value *SyncArg =
1463 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1464 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1465 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001466 }
1467
1468 if (Info.SwitchBlock)
1469 CGF.EmitBlock(Info.SwitchBlock);
1470 if (Info.EndBlock)
1471 CGF.EmitBlock(Info.EndBlock);
1472
1473 // Branch around the rethrow code.
1474 CGF.EmitBranch(FinallyEnd);
1475
1476 CGF.EmitBlock(FinallyRethrow);
1477 CGF.Builder.CreateCall(RethrowFn, CGF.Builder.CreateLoad(RethrowPtr));
1478 CGF.Builder.CreateUnreachable();
1479
1480 CGF.EmitBlock(FinallyEnd);
1481
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001482}
1483
1484void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001485 const ObjCAtThrowStmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001486 llvm::Value *ExceptionAsObject;
1487
1488 std::vector<const llvm::Type*> Args(1, IdTy);
1489 llvm::FunctionType *FTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001490 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattner5dc08672009-05-08 00:11:50 +00001491 llvm::Value *ThrowFn =
1492 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
1493
1494 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1495 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001496 ExceptionAsObject = Exception;
Chris Lattner5dc08672009-05-08 00:11:50 +00001497 } else {
1498 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
1499 "Unexpected rethrow outside @catch block.");
1500 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1501 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001502 ExceptionAsObject =
1503 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Chris Lattner5dc08672009-05-08 00:11:50 +00001504
1505 // Note: This may have to be an invoke, if we want to support constructs like:
1506 // @try {
1507 // @throw(obj);
1508 // }
1509 // @catch(id) ...
1510 //
1511 // This is effectively turning @throw into an incredibly-expensive goto, but
1512 // it may happen as a result of inlining followed by missed optimizations, or
1513 // as a result of stupidity.
1514 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1515 if (!UnwindBB) {
1516 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1517 CGF.Builder.CreateUnreachable();
1518 } else {
1519 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1520 &ExceptionAsObject+1);
1521 }
1522 // Clear the insertion point to indicate we are in unreachable code.
1523 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001524}
1525
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001526llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001527 llvm::Value *AddrWeakObj)
1528{
1529 return 0;
1530}
1531
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001532void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1533 llvm::Value *src, llvm::Value *dst)
1534{
1535 return;
1536}
1537
Fariborz Jahanian58626502008-11-19 00:59:10 +00001538void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1539 llvm::Value *src, llvm::Value *dst)
1540{
1541 return;
1542}
1543
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001544void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1545 llvm::Value *src, llvm::Value *dst)
1546{
1547 return;
1548}
1549
Fariborz Jahanian58626502008-11-19 00:59:10 +00001550void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1551 llvm::Value *src, llvm::Value *dst)
1552{
1553 return;
1554}
1555
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001556void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1557 llvm::Value *DestPtr,
1558 llvm::Value *SrcPtr,
1559 unsigned long size) {
1560 return;
1561}
1562
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001563llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1564 const ObjCInterfaceDecl *ID,
1565 const ObjCIvarDecl *Ivar) {
1566 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1567 + '.' + Ivar->getNameAsString();
1568 // Emit the variable and initialize it with what we think the correct value
1569 // is. This allows code compiled with non-fragile ivars to work correctly
1570 // when linked against code which isn't (most of the time).
1571 llvm::GlobalVariable *IvarOffsetGV = CGM.getModule().getGlobalVariable(Name);
1572 if (!IvarOffsetGV) {
1573 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1574 llvm::ConstantInt *OffsetGuess =
Owen Andersona1cf15f2009-07-14 23:10:40 +00001575 VMContext.getConstantInt(LongTy, Offset, "ivar");
Owen Anderson1c431b32009-07-08 19:05:04 +00001576 IvarOffsetGV = new llvm::GlobalVariable(TheModule, LongTy, false,
1577 llvm::GlobalValue::CommonLinkage, OffsetGuess, Name);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001578 }
1579 return IvarOffsetGV;
1580}
1581
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001582LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1583 QualType ObjectTy,
1584 llvm::Value *BaseValue,
1585 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001586 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001587 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00001588 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1589 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001590}
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001591static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1592 const ObjCInterfaceDecl *OID,
1593 const ObjCIvarDecl *OIVD) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001594 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001595 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001596 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1597 if (OIVD == Ivars[k])
1598 return OID;
1599 }
1600
1601 // Otherwise check in the super class.
1602 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1603 return FindIvarInterface(Context, Super, OIVD);
1604
1605 return 0;
1606}
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001607
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001608llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001609 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001610 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001611 if (CGF.getContext().getLangOptions().ObjCNonFragileABI)
1612 {
1613 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
1614 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
1615 false, "ivar");
1616 }
Daniel Dunbar97776872009-04-22 07:32:20 +00001617 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001618 return VMContext.getConstantInt(LongTy, Offset, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001619}
1620
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001621CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001622 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001623}